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
This code will work for you.
You can try this code
or if you have only 2 Person properties
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#
Output