从 C# 访问 XML 数据
Posted
技术标签:
【中文标题】从 C# 访问 XML 数据【英文标题】:Accessing XML data from C# 【发布时间】:2015-01-06 22:59:18 【问题描述】:我正在努力从 C# 访问存储在 XML 文件中的数据。我阅读了很多帖子 - 最接近的帖子是 here - 但仍然无法正常工作。
xml 节点如下所示:
<Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.8032865</Value>
</Prop>
我需要知道名为“StartTime”的属性的值是什么
编辑:上面的节点是 xml 文档中的众多节点之一。它嵌套在其他节点之间。我需要找到名称为“StartTime”的属性的所有值。
我使用 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>
【问题讨论】:
当然是171.8032865! (你试过什么?) 一种方法是查看根中所有名为“Prop”的元素并检查它们是否具有属性,可能是属性值为“StartTime”的属性“Name”。然后检查该元素是否具有子元素“值”。如果是,则返回“Value”的值。可能有 Linq to Xml 的东西可以做到这一点。 【参考方案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“System.Xml.Linq.XContainer.Elements()”是一个“方法”,在给定的上下文中无效。我在 XAML 中的 Button_click 事件中调用它。 将 Elements 更改为 Elements(),我的错误,现在编辑我的帖子。 由于忘记解析 Value 元素,我做了一些修改。您可以参考下面的 Kaz 示例以获得一个很好的解决方案,但是如果 Prop StartTime 或其子值元素不在 xml 响应或空值中,它可能会出错。这就是为什么我按照我在回答中所做的那样做。至少据我所知,linq 不会自动处理空值和缺失节点。 好的,更好,但我仍然没有得到我想要的。我编辑了我的帖子,指出我有很多这样的节点要搜索,所以我的 xml 更大。我必须找到的节点也是嵌套的。如果我加载并运行它,我会得到 NullReferenceException: Object reference not set to an instance of an object。【参考方案2】:您的案例您可以使用下面的代码来获取价值
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);
【讨论】:
【参考方案3】:只需使用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();
【讨论】:
【参考方案4】:好的....获取整个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();
这给了我一个包含值的列表:
“171.6948629” “171.8032865” “171.8135159” “171.8240739” “171.8270851”这感觉更像您正在寻找的东西。
【讨论】:
对象引用未设置为对象的实例。指向 x => x.Attribute("Name").Value == "StartTime" @user1146081 你是对的。如果“值”不存在,则会出错。更新了我的解决方案,现在应该可以工作了。 再次:对象引用未设置为对象的实例。这次在 if 条件下指向 x.Attribute("Name").Value == "StartTime"。 Kaz,刚刚编辑了我最初的帖子。看看我的 xml。 好的!作品。非常感谢 Kaz 的努力。你知道我在哪里可以更系统地学习从 C# 访问 xmls 吗?【参考方案5】:您可能想查看 Visual Studio XSD 工具。有关如何从命令行或使用Paste Special
菜单选项运行它的更多信息,请参阅此Question。
该工具将直接从 XML 生成 C# 类,然后您可以序列化并以编程方式访问。
编辑
从技术上讲,这不是一个直接的答案(请随意投反对票)。但是,它确实提供了一种替代解决方案,可以帮助解决更大的 XML 属性读写问题。
【讨论】:
【参考方案6】: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);
你可以试试这个
【讨论】:
以上是关于从 C# 访问 XML 数据的主要内容,如果未能解决你的问题,请参考以下文章