Reputation: 65
I've been struggling trying to parse with Linq (not any attachment to Linq at all, it's just the most used on the samples I found) the XML Soap Response below. I've tried some similar answers already posted on StackOverflow, but none of them deal with these multiple namespaces on different elements of the XML structure. The SOAP response is a mess, I know, but it's given. The only thing I need is the value from the itemID field (namely MyITItemName)
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header
xmlns:ns="http://services.mycompany.biz/it/managementservice/ITSMIT/1/"
xmlns:ns1="http://schemata.mycompany.biz/it/managementservice/ITSMIT/1/"/>
<soapenv:Body
xmlns:ns="http://services.mycompany.biz/it/managementservice/ITSMIT/1/"
xmlns:ns1="http://schemata.mycompany.biz/it/managementservice/ITSMIT/1/">
<ns1:createItemResponse
xmlns:ns1="http://services.mycompany.biz/it/managementservice/ITSMIT/1/">
<ns2:id
xmlns:ns2="http://schemata.mycompany.biz/it/managementservice/ITSMIT/1/">338632
</ns2:id>
<ns2:itemID
xmlns:ns2="http://schemata.mycompany.biz/it/managementservice/ITSMIT/1/">MyITItemName
</ns2:itemID>
</ns1:createItemResponse>
</soapenv:Body>
</soapenv:Envelope>
Can anyone shed a light (or some code :-) ) on this one?
Upvotes: 0
Views: 644
Reputation: 4903
You can get the itemId by using Linq to XML especially XDocument :
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
<soapenv:Header
xmlns:ns=""http://services.mycompany.biz/it/managementservice/ITSMIT/1/""
xmlns:ns1=""http://schemata.mycompany.biz/it/managementservice/ITSMIT/1/""/>
<soapenv:Body
xmlns:ns=""http://services.mycompany.biz/it/managementservice/ITSMIT/1/""
xmlns:ns1=""http://schemata.mycompany.biz/it/managementservice/ITSMIT/1/"">
<ns1:createItemResponse
xmlns:ns1=""http://services.mycompany.biz/it/managementservice/ITSMIT/1/"">
<ns2:id xmlns:ns2=""http://schemata.mycompany.biz/it/managementservice/ITSMIT/1/"">338632</ns2:id>
<ns2:itemID xmlns:ns2=""http://schemata.mycompany.biz/it/managementservice/ITSMIT/1/"">MyITItemName</ns2:itemID>
</ns1:createItemResponse>
</soapenv:Body>
</soapenv:Envelope>";
XNamespace ns2 = "http://schemata.mycompany.biz/it/managementservice/ITSMIT/1/";
string itemId = XDocument.Parse(xml)
.Descendants(ns2 + "itemID")
.FirstOrDefault()
.Value;
I hope you find this helpful.
Upvotes: 1
Reputation: 179
So if you know that you are always looking for ns2:itemID (which the MyITItemName is within), you will need to access the childnodes. I have a working example here below. I took your xml and saved it as an xml file in my downloads folder.
void Main()
{
// replace this configFile location to where you save your xml file
GetClientNoteXmlData("MyITItemName", @"C:\Users\first.last\Downloads\test.xml");
}
public void GetClientNoteXmlData(string client, string configFile)
{
XmlDocument doc = new XmlDocument();
doc.Load(configFile);
XmlElement root = doc.DocumentElement;
if (root != null)
{
XmlNodeList list = root.GetElementsByTagName("ns2:itemID");
foreach (XmlNode node in list)
{
Console.WriteLine(node.InnerXml);
Console.WriteLine(node.OuterXml);
}
}
}
Upvotes: 1