XML SelectSingleNode failing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: XML SelectSingleNode failing

  1. #1
    Join Date
    Jan 2011
    Posts
    5

    Exclamation XML SelectSingleNode failing

    Hi guys,

    I have a XML file that I'm reading as a string and then searching into. The problem is that the SelectSingleNode is failing for any non-root node.

    To access the <HostName> value, I use
    string xmlFile = "<?xml ..............................."; //Has the XML data
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlFile);

    XmlElement root = doc.DocumentElement;
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
    nsmgr.AddNamespace("config", "http://tempuri.org/ConfigSchema.xsd");

    XmlNode test = root.SelectSingleNode("//config:HostName", nsmgr);
    Console.WriteLine(test.InnerText);

    The above works and prints out "test123" but if I want to select <NIC0><Name>, what do I write? The below failed:

    XmlNode test = root.SelectSingleNode("//config:NIC0/Name", nsmgr);

    My XML is:
    <?xml version="1.0" encoding="utf-16"?>
    <Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/ConfigSchema.xsd">
    <ConfigVersion>V2.0</ConfigVersion>
    <ConfigHash>E9D9A</ConfigHash>
    <HostName>test123</HostName>

    <NIC0>
    <Active>True</Active>
    <Name>Intel(R) 82567LM Gigabit Network Connection</Name>
    <IP>192.126.3.3</IP>
    <MAC>00:1C:25:24:14:R8</MAC>
    <Manufacturer />
    </NIC0>
    </Configuration>

  2. #2
    Join Date
    Jan 2011
    Posts
    5

    [RESOLVED]: XML SelectSingleNode failing

    I resolved it by using

    XmlNode test = root.SelectSingleNode("/config:Configuration/config:NIC0/config:Name", nsmgr);


    Hope this helps someone!

    -Nick

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center