locked
Get to node within XML using Linq to XML RRS feed

  • Question

  • User-284642143 posted

    My XML

    <?xml version="1.0" encoding="utf-8"?>
    <customers xmlns="http://site.com/ns/" si="0" records="2">
    <customer id="2345">
        <reference>ABCDEF</reference>
        <querydate>2017-12-10T15:00:14.044Z</querydate>
        <contact>
          <telnumber>1234567890</telnumber>
        </contact>
        <link rel="web" href="http://www.customer.com" />
    </customer>
    <customer id="2346">
        <reference>ABCDEF</reference>
        <querydate>2017-12-10T15:00:14.044Z</querydate>
        <contact>
          <telnumber>1234567890</telnumber>
        </contact>
        <link rel="web" href="http://www.customer.com" />
    </customer>
      <link rel="self" href="http://site.com" />
    </customers>

    I'm trying to get the contact number so have this Linq to XML

            Dim x As XElement = Xelement.Load("E:\XMLFile1.xml")
            Dim TopElements As IEnumerable(Of XElement) = Xelement.Elements
    
            For Each Cs In TopElements
                Dim c As String = Cs.Element("contact").Element("telnumber").Value
            Next

    This didnt work (even though i was under the customer node)
    So i saw an example which gets it at TopElements so no need to have a For Each

    Dim Contacts = From c In TopElements Where (c.Element("contact").Element("telnumber")) Select c

    but again this returns null. How could i target the contact section only?

    Monday, January 29, 2018 1:07 PM

Answers

  • User-707554951 posted

    Hi EssCee

    Because this xml added xmlns namespace, so if we use SelectNodes, result will be null

    Working code as below:

    Xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <customers xmlns="http://www.w3.org/TR/html4/" si="0" records="2" >
      <customer id="2345">
        <reference>ABCDEF</reference>
        <querydate>2017-12-10T15:00:14.044Z</querydate>
        <contact>
          <telnumber>1234567890</telnumber>
        </contact>
        <link rel="web" href="http://www.customer.com" />
      </customer>
      <customer id="2346">
        <reference>ABCDEF</reference>
        <querydate>2017-12-10T15:00:14.044Z</querydate>
        <contact>
          <telnumber>1234567890</telnumber>
        </contact>
        <link rel="web" href="http://www.customer.com" />
      </customer>
      <link rel="self" href="http://site.com" />
    </customers>

    Code:

      protected void Page_Load(object sender, EventArgs e)
            {
               
                XmlReaderSettings xs = new XmlReaderSettings();
                xs.XmlResolver = null;
                xs.ProhibitDtd = false;
                XmlReader xr = XmlReader.Create(Server.MapPath("XMLFile1.xml"), xs);
                XmlDocument doc = new XmlDocument();
                doc.Load(xr);//载入源XML文件
                XmlNamespaceManager nameSpace = new XmlNamespaceManager(doc.NameTable);
                nameSpace.AddNamespace("abc","http://www.w3.org/TR/html4/");
                XmlElement xmlFile = doc.DocumentElement;
                var result =xmlFile.SelectNodes("abc:customer/abc:contact/abc:telnumber", nameSpace);
                foreach (XmlElement xe in result)
                {
                    Response.Write(xe.InnerText + "<br />");
                }
            }

    Output:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 30, 2018 10:04 AM