I'd like to use LINQ to XML to extract values from an XML doc that has nested namespaces.
My question had partially been answered by another SO user: LINQ to XML w/ nested namespaces
I am still having trouble trying to figure out how to select the values into an object's properties.
Here are the resources for this problem:
Contents of above link:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:Envelope xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns3="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4">
<ns1:Header>
<ns3:TransactionID ns1:mustUnderstand="1">XXXX-XXXX-XXXX-XXXX</ns3:TransactionID>
</ns1:Header>
<ns1:Body>
<ns3:DeliverReq>
<ns3:MM7Version>6.8.0</ns3:MM7Version>
<ns3:LinkedID>LINKED-ID-IS-HERE</ns3:LinkedID>
<ns3:Sender>
<ns3:Number>3025551212</ns3:Number>
</ns3:Sender>
<ns3:Recipients>
<ns3:To>
<ns3:Number displayOnly="false">11111</ns3:Number>
</ns3:To>
</ns3:Recipients>
<ns3:TimeStamp>2011-04-25T10:28:40.000Z</ns3:TimeStamp>
<ns3:UACapabilities UAProf="motok1c"/>
<ns3:Content allowAdaptations="true" href="cid:default.cid"/>
</ns3:DeliverReq>
</ns1:Body>
</ns1:Envelope>
Object to be filled from this XML:
public class TestClass
{
public string TransactionId { get; set; }
public string MessageType { get; set; }
public string Mm7Version { get; set; }
public string VaspId { get; set; }
public string VasId { get; set; }
public string Sender { get; set; }
public string Recipients { get; set; }
public string LinkedId { get; set; }
public string TimeStamp { get; set; }
public string Priority { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public string UaCapabilities { get; set; }
// not all properties appear in every XML message received
}
The SO example helped me understand the need to add a namespace (XNamespace), but I am falling short when trying to fill the object using a LINQ statement.
I think I need to do something like:
// (don't know the "from" in this case - decendants?
// which XML key / node?)
select new TestClass
{
TransactionId = [LINQ to select the "TransactionID" value that
appears under the Header key]
Mm7Version = [LINQ to select the "MM7Version" value, under the
DeliverReq key]
.....
}
What is the shape of the LINQ to be able to select values into properties of an object when you have XML like posted above? I care about the data in 2 portions of the XML: The TransactionID value in the header, and the values that are under the DeliverReq. It is odd to be that they are spread out and it is more confusing than simply selecting the values strictly from the DeliverReq key. If I saw the FROM portion, and 2 to 3 of the properties filled with values from the XML then I would be able to pick it up from there.
Please let me know if you need any clarifications. My main problems are with the "FROM" portion of the LINQ and how to deal with the fact that TransactionId is outside of the DeliverReq tag. I think I can handle the other cases, such as the nesting on the values under the Sender and Recipients tags.