读取多个XML属性

时间:2016-08-23 12:48:26

标签: c# xml

我正在尝试使用Visual Studio中的C#从XML库(下面的文件)中获取信息。

<buttons>
<measurement>
    <scan id="0" time="20 53 06">
        <q address="40" state="0"/>
        <q address="41" state="0"/>
        <q address="42" state="1"/>
        <q address="43" state="0"/>
        <q address="44" state="1"/>
        <q address="45" state="1"/>
        <q address="46" state="1"/>
        <q address="47" state="1"/>
    </scan>
</measurement>
<measurement>
    <scan id="1" time="20 53 07">
        <q address="40" state="0"/>
        <q address="41" state="0"/>
        <q address="42" state="0"/>
        <q address="43" state="0"/>
        <q address="44" state="1"/>
        <q address="45" state="0"/>
        <q address="46" state="0"/>
        <q address="47" state="0"/>
    </scan>
</measurement>
<measurement>
    <scan id="2" time="20 53 08">
        <q address="40" state="0"/>
        <q address="41" state="1"/>
        <q address="42" state="0"/>
        <q address="43" state="1"/>
        <q address="44" state="1"/>
        <q address="45" state="0"/>
        <q address="46" state="0"/>
        <q address="47" state="1"/>
    </scan>
</measurement>
<measurement>
    <scan id="3" time="20 53 09">
        <q address="40" state="1"/>
        <q address="41" state="0"/>
        <q address="42" state="0"/>
        <q address="43" state="1"/>
        <q address="44" state="1"/>
        <q address="45" state="0"/>
        <q address="46" state="1"/>
        <q address="47" state="0"/>
    </scan>
</measurement>
<measurement>
    <scan id="4" time="20 53 10">
        <q address="40" state="0"/>
        <q address="41" state="0"/>
        <q address="42" state="1"/>
        <q address="43" state="1"/>
        <q address="44" state="1"/>
        <q address="45" state="1"/>
        <q address="46" state="0"/>
        <q address="47" state="0"/>
    </scan>
</measurement>
<measurement>
    <scan id="5" time="20 53 11">
        <q address="40" state="1"/>
        <q address="41" state="1"/>
        <q address="42" state="1"/>
        <q address="43" state="0"/>
        <q address="44" state="0"/>
        <q address="45" state="0"/>
        <q address="46" state="1"/>
        <q address="47" state="0"/>
    </scan>
</measurement>
<measurement>
    <scan id="6" time="20 53 12">
        <q address="40" state="0"/>
        <q address="41" state="1"/>
        <q address="42" state="1"/>
        <q address="43" state="0"/>
        <q address="44" state="0"/>
        <q address="45" state="1"/>
        <q address="46" state="1"/>
        <q address="47" state="1"/>
    </scan>
</measurement>
<measurement>
    <scan id="7" time="20 53 13">
        <q address="40" state="1"/>
        <q address="41" state="1"/>
        <q address="42" state="1"/>
        <q address="43" state="1"/>
        <q address="44" state="1"/>
        <q address="45" state="0"/>
        <q address="46" state="1"/>
        <q address="47" state="1"/>
    </scan>
</measurement>

这是我第一次尝试和阅读xml文件。

Hre是我想做的事:
我想inpud ID和程序schould返回数组中的所有q状态。

以下是我的尝试:

using System.Xml;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Path to my xml");
XmlNodeList titleNodes = xmlDoc.SelectNodes("//buttons/measurment/scan/q");
foreach(XmlNode titleNode in titleNodes)
    Console.WriteLine(titleNode.Attribute["address"]);
Console.ReadKey();

我知道这段代码只会在控制台中显示一些q属性。我从来没有因为它无法正常工作(控制台窗口中没有任何内容)。我不知道为什么。我只是不明白这个XmlRead,XmlDeocument。我在这里学习:Tutorial。我正在接受每一个词干和考试。只有XmlRead给我一些东西。它只读取所有内容,而不是特定区域。

你能告诉我一些例子或解决方案吗?

3 个答案:

答案 0 :(得分:3)

考虑使用XDocument,它允许您使用LINQ,使您的xml生活更轻松:

string myId = "2";

var doc = XDocument.Parse(xmlString); //or XDocument.Load(filePath)

//find the correct 'scan' node based on your id
var scan = doc.Descendants("scan")
              .FirstOrDefault(s => s.Attribute("id").Value == myId);

//grab all q's and get their 'state' for that 'scan' node
var states = scan?.Descendants("q")
                  .Select(q => q.Attribute("state").Value);

foreach (var state in states)
{
    Console.WriteLine(state);
}

答案 1 :(得分:1)

假设xml格式正确并且根节点被核心关闭, 您在字符串measurement中输入错误,这是正确选择所有q代码

XmlNodeList titleNodes = xmlDoc.SelectNodes("//buttons/measurement/scan/q");

如果您只想要一个ID以下的那些

XmlNodeList titleNodes = xmlDoc.SelectNodes("//buttons/measurement/scan[@id=2]/q") ;

如果没有这样的id,你将获得一个空集合但不是异常错误:这样就可以管理这个案例。

最后,不要忘记Value字段

Console.WriteLine(titleNode.Attributes["address"].Value);

答案 2 :(得分:0)

XmlNodeList titleNodes = xmlDoc.SelectNodes("//measurement/scan/q");

然后你会得到64件物品。