从C#访问XML数据

本文关键字:数据 XML 访问 | 更新日期: 2023-09-27 18:29:49

我很难从C#访问存储在XML文件中的数据。我读了很多帖子——最近的一篇在这里——但仍然无法让它发挥作用。

xml节点如下所示:

<Prop Name='StartTime' Type='Number' Flags='0x0'>
<Value>171.8032865</Value>
</Prop>

我需要知道名为"StartTime"的属性的值是多少

EDIT:上面的节点是xml文档中的众多节点之一。并且它嵌套在其他节点之间。我需要查找名为"StartTime"的属性的所有值。

我使用XAML。

编辑2:我认为如果我显示更大的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>

从C#访问XML数据

好的。。。。在得到整个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"

这感觉更像是你在寻找的东西。

只需使用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();
        }
    }
}

您的案例可以使用下面的代码来获取值

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);
        }

您可能需要查看Visual Studio XSD工具。有关如何从命令行或使用Paste Special菜单选项运行它的更多信息,请参阅此问题。

该工具将直接从XML生成C#类,然后您可以用程序对其进行序列化和访问。

编辑

从技术上讲,这不是一个直接的答案(可以随意投反对票)。然而,它确实提供了一种替代解决方案,可以帮助解决读取和写入XML属性这一更大的问题。

您可以使用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);
    }
}
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);
        }
    }
}

你可以试试这个