我正在努力从C#访问存储在XML文件中的数据。我阅读了很多帖子 - 最接近的帖子是here - 但仍然无法让它发挥作用。
xml节点如下所示:
<Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.8032865</Value>
</Prop>
我需要知道名为&#39; StartTime&#39;
的属性的值编辑:上面的节点是xml文档中的众多节点之一。它嵌套在其他节点中。我需要找到名称为&#39; StartTime&#39;。
的所有属性值我使用XAML。
EDIT2:我认为如果我显示更大的xml块,我必须从中获取数据。看起来Kaz的解决方案是最接近要求的解决方案,但是如果我加载整个xml它就会失败。
XML文件:
<Prop Type='TEResult' Flags='0x0'>
<Prop Name='TS' Type='Obj' Flags='0x0'>
<Prop Name='SequenceCall' Type='Obj' Flags='0x0'>
<Prop Name='ResultList' Type='Array' LBound='[0]' HBound='[3]' ElementType='Obj' Flags='0x0'>
<ArrayElementPrototype Type='TEResult' Flags='0x0'>
</ArrayElementPrototype>
<Value ID='[0]'>
<Prop Type='TEResult' Flags='0x0'>
<Prop Name='Error' Type='Obj' TypeName='Error' Flags='0x400000'>
<Prop Name='Code' Type='Number' Flags='0x400000'>
<Value>0</Value>
</Prop>
<Prop Name='Msg' Type='String' Flags='0x400000'>
<Value></Value>
</Prop>
<Prop Name='Occurred' Type='Boolean' Flags='0x400000'>
<Value>False</Value>
</Prop>
</Prop>
<Prop Name='Status' Type='String' Flags='0x400000'>
<Value>Failed</Value>
</Prop>
<Prop Name='Numeric' Type='Number' Flags='0x2400'>
<Value>0</Value>
</Prop>
<Prop Name='ReportText' Type='String' Flags='0x400000'>
<Value></Value>
</Prop>
<Prop Name='Common' Type='Obj' TypeName='CommonResults' Flags='0x0'>
<Prop Name='IncludeInReport' Type='Boolean' Flags='0x0'>
<Value>True</Value>
</Prop>
</Prop>
<Prop Name='TS' Type='Obj' Flags='0x0'>
<Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.8032865</Value>
</Prop>
<Prop Name='TotalTime' Type='Number' Flags='0x0'>
<Value>0.0005158</Value>
</Prop>
<Prop Name='Index' Type='Number' Flags='0x0'>
<Value>0</Value>
</Prop>
<Prop Name='StepName' Type='String' Flags='0x0'>
<Value>Test 1</Value>
</Prop>
<Prop Name='StepGroup' Type='String' Flags='0x0'>
<Value>Main</Value>
</Prop>
<Prop Name='StepId' Type='String' Flags='0x0'>
<Value>ID#:TbZA+59stkivP1pqe//sUB</Value>
</Prop>
<Prop Name='Id' Type='Number' Flags='0x0'>
<Value>5</Value>
</Prop>
<Prop Name='StepType' Type='String' Flags='0x0'>
<Value>NumericLimitTest</Value>
</Prop>
<Prop Name='StepCausedSequenceFailure' Type='Boolean' Flags='0x0'>
<Value>True</Value>
</Prop>
<Prop Name='BlockLevel' Type='Number' Flags='0x0'>
<Value>0</Value>
</Prop>
</Prop>
<Prop Name='Limits' Type='Obj' Flags='0x3000'>
<Prop Name='Low' Type='Number' Flags='0x0'>
<Value>9</Value>
</Prop>
<Prop Name='High' Type='Number' Flags='0x0'>
<Value>11</Value>
</Prop>
</Prop>
<Prop Name='Comp' Type='String' Flags='0x3000'>
<Value>GELE</Value>
</Prop>
</Prop>
</Value>
<Value ID='[1]'>
<Prop Type='TEResult' Flags='0x0'>
<Prop Name='Error' Type='Obj' TypeName='Error' Flags='0x400000'>
<Prop Name='Code' Type='Number' Flags='0x400000'>
<Value>0</Value>
</Prop>
<Prop Name='Msg' Type='String' Flags='0x400000'>
<Value></Value>
</Prop>
<Prop Name='Occurred' Type='Boolean' Flags='0x400000'>
<Value>False</Value>
</Prop>
</Prop>
<Prop Name='Status' Type='String' Flags='0x400000'>
<Value>Failed</Value>
</Prop>
<Prop Name='Numeric' Type='Number' Flags='0x2400'>
<Value>0</Value>
</Prop>
<Prop Name='ReportText' Type='String' Flags='0x400000'>
<Value></Value>
</Prop>
<Prop Name='Common' Type='Obj' TypeName='CommonResults' Flags='0x0'>
<Prop Name='IncludeInReport' Type='Boolean' Flags='0x0'>
<Value>True</Value>
</Prop>
</Prop>
<Prop Name='TS' Type='Obj' Flags='0x0'>
<Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.8135159</Value>
</Prop>
<Prop Name='TotalTime' Type='Number' Flags='0x0'>
<Value>0.0000802</Value>
</Prop>
<Prop Name='Index' Type='Number' Flags='0x0'>
<Value>1</Value>
</Prop>
<Prop Name='StepName' Type='String' Flags='0x0'>
<Value>Test 2</Value>
</Prop>
<Prop Name='StepGroup' Type='String' Flags='0x0'>
<Value>Main</Value>
</Prop>
<Prop Name='StepId' Type='String' Flags='0x0'>
<Value>ID#:7RheSM0MDEGTAnRrrfGNcB</Value>
</Prop>
<Prop Name='Id' Type='Number' Flags='0x0'>
<Value>6</Value>
</Prop>
<Prop Name='StepType' Type='String' Flags='0x0'>
<Value>NumericLimitTest</Value>
</Prop>
<Prop Name='StepCausedSequenceFailure' Type='Boolean' Flags='0x0'>
<Value>False</Value>
</Prop>
<Prop Name='BlockLevel' Type='Number' Flags='0x0'>
<Value>0</Value>
</Prop>
</Prop>
<Prop Name='Limits' Type='Obj' Flags='0x3000'>
<Prop Name='Low' Type='Number' Flags='0x0'>
<Value>9</Value>
</Prop>
<Prop Name='High' Type='Number' Flags='0x0'>
<Value>11</Value>
</Prop>
</Prop>
<Prop Name='Comp' Type='String' Flags='0x3000'>
<Value>GELE</Value>
</Prop>
</Prop>
</Value>
<Value ID='[2]'>
<Prop Type='TEResult' Flags='0x0'>
<Prop Name='Error' Type='Obj' TypeName='Error' Flags='0x400000'>
<Prop Name='Code' Type='Number' Flags='0x400000'>
<Value>0</Value>
</Prop>
<Prop Name='Msg' Type='String' Flags='0x400000'>
<Value></Value>
</Prop>
<Prop Name='Occurred' Type='Boolean' Flags='0x400000'>
<Value>False</Value>
</Prop>
</Prop>
<Prop Name='Status' Type='String' Flags='0x400000'>
<Value>Failed</Value>
</Prop>
<Prop Name='Numeric' Type='Number' Flags='0x2400'>
<Value>0</Value>
</Prop>
<Prop Name='ReportText' Type='String' Flags='0x400000'>
<Value></Value>
</Prop>
<Prop Name='Common' Type='Obj' TypeName='CommonResults' Flags='0x0'>
<Prop Name='IncludeInReport' Type='Boolean' Flags='0x0'>
<Value>True</Value>
</Prop>
</Prop>
<Prop Name='TS' Type='Obj' Flags='0x0'>
<Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.8240739</Value>
</Prop>
<Prop Name='TotalTime' Type='Number' Flags='0x0'>
<Value>0.000081</Value>
</Prop>
<Prop Name='Index' Type='Number' Flags='0x0'>
<Value>2</Value>
</Prop>
<Prop Name='StepName' Type='String' Flags='0x0'>
<Value>Test 3</Value>
</Prop>
<Prop Name='StepGroup' Type='String' Flags='0x0'>
<Value>Main</Value>
</Prop>
<Prop Name='StepId' Type='String' Flags='0x0'>
<Value>ID#:pWFWDejvo0GThcOYVssHBA</Value>
</Prop>
<Prop Name='Id' Type='Number' Flags='0x0'>
<Value>7</Value>
</Prop>
<Prop Name='StepType' Type='String' Flags='0x0'>
<Value>NumericLimitTest</Value>
</Prop>
<Prop Name='StepCausedSequenceFailure' Type='Boolean' Flags='0x0'>
<Value>False</Value>
</Prop>
<Prop Name='BlockLevel' Type='Number' Flags='0x0'>
<Value>0</Value>
</Prop>
</Prop>
<Prop Name='Limits' Type='Obj' Flags='0x3000'>
<Prop Name='Low' Type='Number' Flags='0x0'>
<Value>9</Value>
</Prop>
<Prop Name='High' Type='Number' Flags='0x0'>
<Value>11</Value>
</Prop>
</Prop>
<Prop Name='Comp' Type='String' Flags='0x3000'>
<Value>GELE</Value>
</Prop>
</Prop>
</Value>
<Value ID='[3]'>
<Prop Type='TEResult' Flags='0x0'>
<Prop Name='Error' Type='Obj' TypeName='Error' Flags='0x400000'>
<Prop Name='Code' Type='Number' Flags='0x400000'>
<Value>0</Value>
</Prop>
<Prop Name='Msg' Type='String' Flags='0x400000'>
<Value></Value>
</Prop>
<Prop Name='Occurred' Type='Boolean' Flags='0x400000'>
<Value>False</Value>
</Prop>
</Prop>
<Prop Name='Status' Type='String' Flags='0x400000'>
<Value>Failed</Value>
</Prop>
<Prop Name='Numeric' Type='Number' Flags='0x2400'>
<Value>0</Value>
</Prop>
<Prop Name='ReportText' Type='String' Flags='0x400000'>
<Value></Value>
</Prop>
<Prop Name='Common' Type='Obj' TypeName='CommonResults' Flags='0x0'>
<Prop Name='IncludeInReport' Type='Boolean' Flags='0x0'>
<Value>True</Value>
</Prop>
</Prop>
<Prop Name='TS' Type='Obj' Flags='0x0'>
<Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.8270851</Value>
</Prop>
<Prop Name='TotalTime' Type='Number' Flags='0x0'>
<Value>0.0001041</Value>
</Prop>
<Prop Name='Index' Type='Number' Flags='0x0'>
<Value>3</Value>
</Prop>
<Prop Name='StepName' Type='String' Flags='0x0'>
<Value>Test 4</Value>
</Prop>
<Prop Name='StepGroup' Type='String' Flags='0x0'>
<Value>Main</Value>
</Prop>
<Prop Name='StepId' Type='String' Flags='0x0'>
<Value>ID#:w+ziJ/u2bkWz+F3kWa4mpC</Value>
</Prop>
<Prop Name='Id' Type='Number' Flags='0x0'>
<Value>8</Value>
</Prop>
<Prop Name='StepType' Type='String' Flags='0x0'>
<Value>NumericLimitTest</Value>
</Prop>
<Prop Name='StepCausedSequenceFailure' Type='Boolean' Flags='0x0'>
<Value>False</Value>
</Prop>
<Prop Name='BlockLevel' Type='Number' Flags='0x0'>
<Value>0</Value>
</Prop>
</Prop>
<Prop Name='Limits' Type='Obj' Flags='0x3000'>
<Prop Name='Low' Type='Number' Flags='0x0'>
<Value>9</Value>
</Prop>
<Prop Name='High' Type='Number' Flags='0x0'>
<Value>11</Value>
</Prop>
</Prop>
<Prop Name='Comp' Type='String' Flags='0x3000'>
<Value>GELE</Value>
</Prop>
</Prop>
</Value>
</Prop>
<Prop Name='SequenceFile' Type='String' Flags='0x0'>
<Value>C:\Users\Public\Documents\National Instruments\TestStand 2010 SP1\Sequence File 16.seq</Value>
</Prop>
<Prop Name='Sequence' Type='String' Flags='0x0'>
<Value>MainSequence</Value>
</Prop>
<Prop Name='Status' Type='String' Flags='0x0'>
<Value>Failed</Value>
</Prop>
</Prop>
<Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.6948629</Value>
</Prop>
<Prop Name='TotalTime' Type='Number' Flags='0x0'>
<Value>0.1359434</Value>
</Prop>
<Prop Name='ModuleTime' Type='Number' Flags='0x0'>
<Value>0.1357157</Value>
</Prop>
<Prop Name='Index' Type='Number' Flags='0x0'>
<Value>31</Value>
</Prop>
<Prop Name='StepName' Type='String' Flags='0x0'>
<Value>MainSequence Callback</Value>
</Prop>
<Prop Name='StepGroup' Type='String' Flags='0x0'>
<Value>Main</Value>
</Prop>
<Prop Name='StepId' Type='String' Flags='0x0'>
<Value>ID#:xnW0PT0ORE2el7wF7uaxyB</Value>
</Prop>
<Prop Name='Id' Type='Number' Flags='0x0'>
<Value>4</Value>
</Prop>
<Prop Name='StepType' Type='String' Flags='0x0'>
<Value>SequenceCall</Value>
</Prop>
<Prop Name='BlockLevel' Type='Number' Flags='0x0'>
<Value>0</Value>
</Prop>
<Prop Name='SequenceFilePostResultListEntry' Type='Obj' Flags='0x0'>
<Prop Name='ResultList' Type='Array' LBound='[0]' HBound='[]' ElementType='Obj' Flags='0x0'>
<ArrayElementPrototype Type='TEResult' Flags='0x0'>
</ArrayElementPrototype>
</Prop>
<Prop Name='SequenceFile' Type='String' Flags='0x0'>
<Value>C:\Program Files\National Instruments\TestStand 2010 SP1\Components\Models\TestStandModels\SequentialModel.seq</Value>
</Prop>
<Prop Name='Sequence' Type='String' Flags='0x0'>
<Value>SequenceFilePostResultListEntry</Value>
</Prop>
<Prop Name='Status' Type='String' Flags='0x0'>
<Value>Passed</Value>
</Prop>
</Prop>
</Prop>
答案 0 :(得分:1)
您可以使用XDocument来解析xml,
XDocument doc = XDocument.Load("c:\\somexmlfile.xml");
XElement xProp = doc.Root.Elements().Where(p => p.Attribute("Name").Value == "StartTime").FirstOrDefault();
if (xProp != null)
{
XElement xValue = xProp.Element("Value");
if (xValue != null && !string.IsNullOrEmpty(xValue.Value))
{
double startTimeValue = double.Parse(xValue.Value);
}
}
答案 1 :(得分:1)
您的情况下,您可以使用以下代码获取价值
string xml = @"<root><Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.8032865</Value>
</Prop></root>";
var doc = XDocument.Parse(xml);
foreach (var obj in doc.Descendants("Prop"))
{
Console.WriteLine(obj.Element("Value").Value);
}
您的Xml不理想,理想情况下Xml和代码应如下所示
string xml = @"<root><Prop Name='StartTime' Type='Number' Flags='0x0'>171.8032865</Prop>
<Prop Name='StartTime' Type='Number' Flags='0x0'>172.8032865</Prop>
</root>";
var doc = XDocument.Parse(xml);
foreach (var obj in doc.Descendants("Prop"))
{
Console.WriteLine(obj.Value);
}
答案 2 :(得分:1)
只需使用XmlDocument
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace DemoApp
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"
<Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.8032865</Value>
</Prop>");
XmlElement element = doc.DocumentElement;
XmlNode valueNode = element.ChildNodes[0];
double value = double.Parse(valueNode.InnerText);
Console.ReadLine();
}
}
}
答案 3 :(得分:1)
Ok ....获取整个xml后...
XDocument xmlDoc = XDocument.Load(@"Your file path");
var startTimes = xmlDoc.Descendants()
.Where(x => x.Attributes().Any(att => att.Name == "Name" && att.Value == "StartTime"))
.Select(x => x.Value).ToList();
这给了我一个包含值的列表:
这感觉更像你正在寻找的东西。
答案 4 :(得分:1)
您可能希望查看Visual Studio XSD工具。有关如何从命令行或使用Paste Special
菜单选项运行它的详细信息,请参阅此Question。
该工具将直接从XML生成C#类,然后您可以以编程方式对其进行序列化和访问。
修改强>
从技术上讲,这不是一个直接的答案(随时可以选择downvote)。但是,它确实提供了一种替代解决方案,可以帮助解决读取和编写XML属性的更大问题。
答案 5 :(得分:0)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XMLdata
{
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load("c:\\somexmlfile.xml");
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml (doc.ToString());
XmlElement element = doc1.DocumentElement;
XmlNode node= element.ChildNodes[0];
double value = double.Parse(node.InnerText);
}
}
}
您可以尝试