有人可以帮我解决这个XML文件中的4.247的“值Y”。我搜索过以前的线程,但似乎无法将它们应用于此结构。
<Chart1>
<Chart1_SeriesGroup_Collection>
<Chart1_SeriesGroup Label="MP_Trend_Data\MP_Prog/MP_DAILY_FLOW">
<Chart1_CategoryGroup_Collection>
<Chart1_CategoryGroup Label="12/24/2017 1:58:19 AM">
<Value X="0001-01-01T10:30:00+10:30"/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label="12/24/2017 1:58:19 AM">
<Value X="0001-01-01T10:30:00+10:30"/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label="12/24/2017 1:59:19 AM">
<Value X="0001-01-01T10:30:00+10:30"/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label="12/24/2017 1:59:19 AM">
<Value X="0001-01-01T10:30:00+10:30"/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label="12/24/2017 1:58:19 AM">
<Value Y="4.24700021743774" X="2017-12-24T12:28:19.333+10:30"/>
</Chart1_CategoryGroup>
代码我试过了:
var xDoc = XDocument.Load("C:\\attachment\\Flow.xml");
XmlDocument xml = new XmlDocument();
xml.LoadXml(xDoc.ToString());
XmlNodeList xnList = xml.SelectNodes("/Chart1/Chart1_SeriesGroup_Collection/Chart1_SeriesGroup[@Label='MP_Trend_Data\\MP_Prog/MP_DAILY_FLOW']");
foreach (XmlNode Node in xnList)
{
XmlNodeList ynList = Node.SelectNodes("/Chart1_CategoryGroup_Collection/Chart1_CategoryGroup/Value");
foreach (XmlNode Node2 in ynList)
{
textBox1.Text = Node2.Attributes["Y"].Value.ToString();
}
}
答案 0 :(得分:0)
var xmlStr = @"
<Chart1>
<Chart1_SeriesGroup_Collection>
<Chart1_SeriesGroup Label='MP_Trend_Data\MP_Prog/MP_DAILY_FLOW'>
<Chart1_CategoryGroup_Collection>
<Chart1_CategoryGroup Label='12/24/2017 1:58:19 AM'>
<Value X='0001-01-01T10:30:00+10:30'/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label='12/24/2017 1:58:19 AM'>
<Value X='0001-01-01T10:30:00+10:30'/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label='12/24/2017 1:59:19 AM'>
<Value X='0001-01-01T10:30:00+10:30'/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label='12/24/2017 1:59:19 AM'>
<Value X='0001-01-01T10:30:00+10:30'/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label='12/24/2017 1:58:19 AM'>
<Value Y='4.24700021743774' X='2017-12-24T12:28:19.333+10:30'/>
</Chart1_CategoryGroup>
</Chart1_CategoryGroup_Collection>>
</Chart1_SeriesGroup>
</Chart1_SeriesGroup_Collection>
</Chart1>";
var xml = XElement.Parse(xmlStr);
var firstY = xml.Descendants("Value").FirstOrDefault(v => v.Attribute("Y") != null);
if (firstY != null)
{
var y = firstY.Attribute("Y").Value;
}
答案 1 :(得分:0)
试试xml linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("Chart1_CategoryGroup").Where(x => x.Element("Value").Attribute("Y") != null).Select(x => new {
label = (DateTime)x.Attribute("Label"),
y = (double)x.Element("Value").Attribute("Y"),
x = (DateTime)x.Element("Value").Attribute("X")
}).ToList();
}
}
}