How to get attribute value using SelectSingleNode in C#?

Posted by Nano HE on Stack Overflow See other posts from Stack Overflow or by Nano HE
Published on 2010-06-09T09:33:14Z Indexed on 2010/06/09 9:42 UTC
Read the original article Hit count: 192

Filed under:
|
|
|

Hello.

I am parsing a xml document, I need find out the gid (an attribute) value (3810).

Based on SelectSingleNode(). I found it is not easy to find the attribute name and it's value.

Can I use this method or I must switch to other way.

Attached my code.

How can I use book obj to get the attribute value3810 for gid. Thank you.

My test.xml file as below

<?xml version="1.0"?>
<root>
<VersionInfo date="2007-11-28" version="1.0.0.2"/>
<Attributes>
<AttrDir name="EFEM" DirID="1">
<AttrDir name="Aligner" DirID="2">
<AttrDir name="SequenceID" DirID="3">
<AttrObj text="Slot01" gid="3810" unit="" scale="1"/>
<AttrObjCount value="1"/>
</AttrDir>
</AttrDir>
</AttrDir>
</Attributes>
</root>

I wrote the test.cs as below

public class Sample
{    
    public static void Main()
    {    
        XmlDocument doc = new XmlDocument();
        doc.Load("test.xml");

        XmlNode book;
        XmlNode root = doc.DocumentElement;

        book = root.SelectSingleNode("Attributes[AttrDir[@name='EFEM']/AttrDir[@name='Aligner']/AttrDir[@name='SequenceID']/AttrObj[@text='Slot01']]");

        Console.WriteLine("Display the modified XML document....");
        doc.Save(Console.Out);
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml