views:

230

answers:

2

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

[Update 06/10/2010]

  1. The xml file is a complex file. Included thousands of gids. But for each of Xpath, the gid is unique.

  2. I load the xml file to a TreeView control. this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);. When treeView1_AfterSelect event occurred, the e.Node.FullPath will return as a String Value.

  3. I parse the string Value e.Node.FullPath. Then I got the member of XPath Above. Then I tried to find which gid item was selected.

I need find the gid value as a return value indeed.

+1  A: 

You can query XmlDocument itself not DocumentRoot:

XmlDocument doc = new XmlDocument();
XmlNode book = doc.SelectSingleNode("..");
if (book != null)
{
    XmlAttribute gid = book.Attributes["gid"];
    if (gid != null)
    {
       string value = gid.Value;
    }
}
abatishchev
Given the code in the question `book.Attributes` is empty (Count = 0).
ChrisF
@abatishchev, Hello. From `book.Attributes.Count = 0`, can't find the `gid` during run time.
Nano HE
@abatishchev. Acturally, there are thousands of gids in my real xml file. I'd like to parse the element layer with `selectSingleNode()`. I think it's a fast method for my case. Then I can find the unique gid.
Nano HE
@Nano: So please describe more what is your main problem finding `gid` using `SelectSingleNode` ?
abatishchev
@abatishchev. Please see my update above. thank you.
Nano HE
+3  A: 

You can write

XmlAttribute gidAttribute = (XmlAttribute)book.Attributes.GetNamedItem("gid");
String gidValue = null;
if (gidAttribute!=null)
    value = gidAttribute.Value;

Alternatively, expand the Xpath to fetch the attribute, e.g.

Attributes[AttrDir[@name='EFEM']/AttrDir[@name='Aligner']/AttrDir[@name='SequenceID']/AttrObj[@text='Slot01']]/@gid

If the @gid is unique, then you can simply use the Xpath

"//AttrObj[@gid='3810']"

To fetch the desired node with the given id. But note that each request will search through the entire document. It will be more efficient to fetch all the nodes, and then put them in a map, keyed by id.

"//AttrObj[@gid]"

Use XmlNode.SelectNodes to fetch a list of all AttrObj with a @gid attribute.

mdma
Given the code in the question book.Attributes is empty (Count = 0).
ChrisF
btw, will throw `NullReferenceException` in case of not found
abatishchev
@mdma, I tried `book = root.SelectSingleNode("Attributes[AttrDir[@name='EFEM']/AttrDir[@name='Aligner']/AttrDir[@name='SequenceID']/AttrObj[@text='Slot01']/@gid]");`. the book obj works well. but Compiler Error for `XmlAttribute gidAttribute = book.Attributes.GetNamedItem("gid");`: *error CS0266: Cannot implicitly convert type 'System.Xml.XmlNode' to 'System.Xml.XmlAttribute'. An explicit conversion exists (are you missing a cast?)*
Nano HE
@mdma, I can't use `"//AttrObj[@gid='3810']"` . Event though gid is unique in my XPath. But the gid value is a return value as I expressed before.
Nano HE
Sorry, I missed out the cast. (Just as the error said.) See the first line of my answer code.
mdma
@mdma After updated my code. Compile successfully but, `(XmlAttribute)book.Attributes.GetNamedItem("gid")` still return **null**. And for **book.Attributes**, I add watch and found `System.Collections.ICollection.Count = 0`.
Nano HE
@mdma, I updated more information above. Thank you.
Nano HE