skip to Main Content

There is an XML string. I can’t figure out how to get only the contents of the node, and then split it into separate Persons.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://">
    <soap:Body>
        <GetJob xmlns="http:// ">
            <GetResult>
                <Result>00</Result>
                <Data>
                    <Person>
                        <Name>Ивано Иван Иванович</Name>
                        <Active>true</Active>
                    </Person>
                    <Person>
                        <Name>Петров Петр Петрович</Name>
                        <Active>true</Active>
                    </Person>
                </Data>
            </GetResult>
        </<GetJob >
    </soap:Body>
</soap:Envelope>

I tried something like

  XElement contacts = XElement.Parse();
contacts.Element(Element("GetJob ").Element("GetResult")

            XElement contacts = XElement.Parse(
                @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://"">
    <soap:Body>
        <GetJob xmlns=""http:// "">
            <GetResult>
                <Result>00</Result>
                <Data>
                    <Person>
                        <Name>Ивано Иван Иванович</Name>
                        <Active>true</Active>
                    </Person>
                    <Person>
                        <Name>Петров Петр Петрович</Name>
                        <Active>true</Active>
                    </Person>
                </Data>
            </GetResult>
        </<GetJob >
    </soap:Body>
</soap:Envelope>");
            IEnumerable<XElement> de =
      from el in contacts.Descendants("soap")
      select el;
            foreach (XElement el in de)
                Console.WriteLine(el);
            XElement contacts = XElement.Parse(
                @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://"">
    <soap:Body>
        <GetJob xmlns=""http:// "">
            <GetResult>
                <Result>00</Result>
                <Data>
                    <Person>
                        <Name>Ивано Иван Иванович</Name>
                        <Active>true</Active>
                    </Person>
                    <Person>
                        <Name>Петров Петр Петрович</Name>
                        <Active>true</Active>
                    </Person>
                </Data>
            </GetResult>
        </<GetJob >
    </soap:Body>
</soap:Envelope>");
            IEnumerable<XElement> de =
      from el in contacts.Descendants("soap")
      select el;
            foreach (XElement el in de)
                Console.WriteLine(el);

Returns an error:"Name cannot begin with the ‘<‘ character, hexadecimal value 0x3C. Line 18, position 11." I tried it using the method of the Doctoral Candidates

3

Answers


  1. This code will work for you.

    var xml = new XmlDocument();
    xml.LoadXml(xmlString);
    var node = xml.GetElementsByTagName("GetResult")[0];
    
    var ls = new List<Person>();
    foreach (XmlNode n in node["Data"].GetElementsByTagName("Person"))
    {
        ls.Add(new Person
        {
           Name =n["Name"].InnerXml,
           Active = n["Active"].InnerXml == "true" ? true : false
        });
    }
    
    Login or Signup to reply.
  2. You can try this code

        XDocument doc = XDocument.Parse(xml);
    
        var personDetails = doc.Descendants()
        .Where(x => x.Name.LocalName == "Person")
        .Select(x => new
        {
             Name = x.Elements().Where(i=> i.Name.LocalName == "Name").First().Value,
             Active = x.Elements().Where(i=> i.Name.LocalName == "Active").First().Value
        }).ToList();
    

    or if you have only 2 Person properties

    var personDetail = doc.Descendants()
        .Where(x => x.Name.LocalName == "Person")
        .Select(x => new
        {
             Name = x.Elements().First().Value,
             Active = x.Elements().Last().Value 
        }).ToList();
    
    Login or Signup to reply.
  3. Please try the following solution based on LINQ to XML API. It is available in the .Net Framework since 2007.

    All XML elements starting from <GetJob xmlns='http://wow2'>, and down below, are bound to the default namespace. So, we need to declare that namespace and use it in the expressions.

    c#

    void Main()
    {
        XDocument xdoc = XDocument.Parse(@"<soap:Envelope xmlns:soap='http://wow'>
                <soap:Body>
                    <GetJob xmlns='http://wow2'>
                        <GetResult>
                            <Result>00</Result>
                            <Data>
                                <Person>
                                    <Name>Ивано Иван Иванович</Name>
                                    <Active>true</Active>
                                </Person>
                                <Person>
                                    <Name>Петров Петр Петрович</Name>
                                    <Active>true</Active>
                                </Person>
                            </Data>
                        </GetResult>
                    </GetJob>
                </soap:Body>
            </soap:Envelope>");
    
        XNamespace ns2 = "http://wow2";
    
        var persons = xdoc.Descendants(ns2 + "Person")
            .Select(x => new
            {
                Name = x.Element(ns2  + "Name").Value,
                Active = x.Element(ns2 + "Active").Value
            }).ToList();
        
        Console.WriteLine(persons);
    }
    

    Output

    Name Active
    Ивано Иван Иванович true
    Петров Петр Петрович true
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search