Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get node value from string xml

Tags:

c#

xmldocument

I have this string XML

string innerXml = @"<detail><WCFFaultExcepcion xmlns=""http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><ErrorId>b7e9d385-9118-4297-baca-db9ab00f3856</ErrorId><Message>Índice fuera de los límites de la matriz.</Message></WCFFaultExcepcion></detail>";

This is the stringXML

<detail>
    <WCFFaultExcepcion xmlns="http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <ErrorId>b7e9d385-9118-4297-baca-db9ab00f3856</ErrorId>
        <Message>Índice fuera de los límites de la matriz.</Message>
    </WCFFaultExcepcion>
</detail>

What I want is to get the value of the detail Tag, I’m trying with this example but all return null o cero count, could you help me?

 private static void Example()
        {
            string innerXml = @"<detail><WCFFaultExcepcion xmlns=""http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><ErrorId>b7e9d385-9118-4297-baca-db9ab00f3856</ErrorId><Message>Índice fuera de los límites de la matriz.</Message></WCFFaultExcepcion></detail>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(innerXml);

            XmlNode node = (XmlNode)doc.DocumentElement;
            XmlNode optionalNode = node.SelectSingleNode("/detail/WCFFaultExcepcion");
            XmlNode optionalNode1 = node.SelectSingleNode("detail/WCFFaultExcepcion");
            XmlNode optionalNode2 = node.SelectSingleNode("/detail/WCFFaultExcepcion/ErrorId");
            XmlNode optionalNode3 = node.SelectSingleNode("detail/WCFFaultExcepcion/ErrorId");
            XmlElement optional = doc.SelectSingleNode(@"/detail/WCFFaultExcepcion/ErrorId") as XmlElement;
            XmlElement optiona2 = doc.SelectSingleNode(@"detail/WCFFaultExcepcion/ErrorId") as XmlElement;
            XmlNode xNode = doc.DocumentElement.SelectNodes("ErrorId")[0];
            XmlNodeList xnList = doc.SelectNodes("/detail/WCFFaultExcepcion");
            XmlNodeList xnList1 = doc.SelectNodes("detail/WCFFaultExcepcion");
            XmlNodeList xnList2 = doc.SelectNodes("/detail/WCFFaultExcepcion/ErrorId");
            XmlNodeList xnList3 = doc.SelectNodes("detail/WCFFaultExcepcion/ErrorId");
        }
like image 238
foluis Avatar asked May 08 '13 23:05

foluis


People also ask

What is XmlNode?

XmlNode is the base class in the . NET implementation of the DOM. It supports XPath selections and provides editing capabilities. The XmlDocument class extends XmlNode and represents an XML document. You can use XmlDocument to load and save XML data.


2 Answers

I think this might be the solution for you:

XmlDocument doc = new XmlDocument();
doc.LoadXml(innerXml);

XmlNodeList ErrorIdTags = doc.GetElementsByTagName("ErrorId");
if(ErrorIdTags.Count <= 1)
{
    // The tag could not be fond
}
else
{
    // The tag could be found!
    string ErrorId = ErrorIdTags[0].InnerText;
}
like image 184
Swen Kooij Avatar answered Sep 22 '22 17:09

Swen Kooij


Try using Linq to Xml (http://msdn.microsoft.com/en-us/library/bb387098%28v=VS.100%29.aspx) - the code will be really elegant.

like image 38
aquaraga Avatar answered Sep 21 '22 17:09

aquaraga