简体   繁体   中英

Reading multiple XML attributes

I'm trying to get information from XML library (file below) using C# in Visual Studio.

<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>

it is my first attempt to do and read xml files.

Hre is what i want to do:
I want to inpud ID and program schould return all q states in array.

Here is what I tried:

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

I know that this code will only display some q atributes in console. I never went furder because it is not working (nothing appears in console window) . And i dont know why. I just don understand this XmlRead, XmlDeocument. I was learnig form here: Tutorial . I was folowing every stem and examlpes. Only XmlRead giave something to me. Bu it reads just everything, not specific regions.

Can you show me some examples or solutions?

Consider using XDocument , which allows you to use LINQ, making your xml life easier:

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

Assuming the xml is well formed and the root node is corectly closed, you have a typo in the string measurement and this is correctly selecting all the q tags

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

If you want only the ones under one id

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

If there is no such id, you'll get an empty collection but not an exception error: so that case is managed.

Finally, don't forget the Value field

Console.WriteLine(titleNode.Attributes["address"].Value);
XmlNodeList titleNodes = xmlDoc.SelectNodes("//measurement/scan/q");

然后,您将获得64件物品。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM