Home > Enterprise >  get specific node from XML using XDOCUMENT
get specific node from XML using XDOCUMENT

Time:10-18

I have an XML document with ISO-8859-1 encoding. I can load the XML with XDOCUMENT, but i can't storage a specific node. Can somebody help me with this? The xml:

<?xml version="1.0" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <TERMEKADATOutput xmlns="http://xmlns.oracle.com/orawsv/PUPHAX/PUPHAXWS">
      <RETURN>
        <OBJTERMEKADAT>              
          <TTT>210037069</TTT>
          <TK>OGYI-T-04617/05</TK>             
        </OBJTERMEKADAT>
      </RETURN>
    </TERMEKADATOutput>
  </soap:Body>
</soap:Envelope>

And the code:

XDocument xmlDoc = null;    
        using (StreamReader oReader = new StreamReader(@"C:\Users\maruzsama\source\repos\EPPuphax\EPPuphax\asd.xml", Encoding.GetEncoding("ISO-8859-1")))
        {
            xmlDoc = XDocument.Load(oReader); //this working fine
            var result = from q in xmlDoc.Descendants("OBJTERMEKADAT")
                         select new
                         {
                             asd = q.Element("TTT").Value
                         };
            Console.ReadKey();
        }

I need the data from the TTT node (210037069)

CodePudding user response:

Try following changes :

XDocument xmlDoc = null;    
        using (StreamReader oReader = new StreamReader(@"C:\Users\maruzsama\source\repos\EPPuphax\EPPuphax\asd.xml", Encoding.GetEncoding("ISO-8859-1")))
        {
            xmlDoc = XDocument.Load(oReader); //this working fine
            XElement TERMEKADATOutput = xmlDoc.Descendants().Where(x => x.Name.LocalName == "TERMEKADATOutput").First();
            XNamespace ns = TERMEKADATOutput.GetDefaultNamespace();
            var result = from q in  TERMEKADATOutput.Descendants(ns   "OBJTERMEKADAT")
                         select new
                         {
                             asd = q.Element(ns   "TTT").Value
                         };
            Console.ReadKey();
        }
  • Related