Reading node in XML file with namespace?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sharon

    Reading node in XML file with namespace?

    I'm trying to navigate inside an XML file that has a very simple namespace,
    But when I'm using the name space a get nothing (Count == 0) when I do
    SelectNodes() or SelectSingleNod e() (null).
    When I remove the namespace from the XML file and from the code --> the
    Select is successful but I do need to leave the namespace in the XML file.

    Here is the XML file I'm using:
    <?xml version="1.0" standalone="yes "?>
    <MyDB xmlns="DataBase .xsd">
    <Subjects>
    <Subject>
    <ID>Cylinder</ID>
    <Defects>
    <Defect>
    <ID>100</ID>
    <Sevirity>10</Sevirity>
    </Defect>
    <Defect>
    <ID>101</ID>
    <Sevirity>20</Sevirity>
    </Defect>
    <Defect>
    <ID>102</ID>
    <Sevirity>30</Sevirity>
    </Defect>
    </Defects>
    </Subject>
    </Subjects>
    </MyDB>

    And the code using DOM:
    XmlDocument doc = new XmlDocument();
    doc.Load("EvsDB Output.xml");
    XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(doc.NameTa ble);
    nsmgr.AddNamesp ace("ns", "DataBase.xsd") ;
    // or // nsmgr.AddNamesp ace("", "DataBase.xsd") ;

    XmlNode node = doc.SelectSingl eNode("EvsDB/Subjects", nsmgr); // return null
    XmlNodeList listSubjects = doc.SelectNodes ("/EvsDB/Subjects", nsmgr); //
    return listSubjects.Co unt == 0
    XmlNodeList listSubject = doc.SelectNodes ("/EvsDB/Subjects/Subject", nsmgr);
    // return listSubject.Cou nt == 0
    XmlNodeList listDefect =
    doc.SelectNodes ("/EvsDB/Subjects/Subject/Defects/Defect", nsmgr); // return
    listDefect.Coun t == 0


    Can anybody tell hove to make it work?



    --------
    Thanks
    Sharon
  • Andy

    #2
    Re: Reading node in XML file with namespace?

    If you go with the ("ns", "DataBase.x sd") manager, you'll need to
    prefix ns: before all of your nodes. For example:

    XmlNode node = doc.SelectSingl eNode("ns:EvsDB/ns:Subjects", nsmgr);

    BTW, i haven't had any luck trying to specify the default namespace...i
    still end up with null nodes. So just assign a prefix and remember to
    use that prefix throughout.

    HTH
    andy

    Comment

    • Sharon

      #3
      Re: Reading node in XML file with namespace?

      It works, thanks a lot.

      BTW: To set a default namespcae, simply do something like:
      XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(doc.NameTa ble);
      nsmgr.AddNamesp ace("", "DataBase.xsd") ;

      --------
      Thanks again
      Sharon


      "Andy" wrote:
      [color=blue]
      > If you go with the ("ns", "DataBase.x sd") manager, you'll need to
      > prefix ns: before all of your nodes. For example:
      >
      > XmlNode node = doc.SelectSingl eNode("ns:EvsDB/ns:Subjects", nsmgr);
      >
      > BTW, i haven't had any luck trying to specify the default namespace...i
      > still end up with null nodes. So just assign a prefix and remember to
      > use that prefix throughout.
      >
      > HTH
      > andy
      >
      >[/color]

      Comment

      • Martin Honnen

        #4
        Re: Reading node in XML file with namespace?



        Sharon wrote:

        [color=blue]
        > BTW: To set a default namespcae, simply do something like:
        > XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(doc.NameTa ble);
        > nsmgr.AddNamesp ace("", "DataBase.xsd") ;[/color]

        No, that does not help, for the XPath expression you need to bind a
        prefix to the default namespace URI if you want to select elements, see
        <http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>

        --

        Martin Honnen --- MVP XML

        Comment

        Working...