0

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:

Sample XML File Link

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.

1 Answer 1

1

Since there is only properties for one of your TestClass instances in your XML, and the values for those are all over the place it makes sense to select them manually and then create the instance:

XDocument doc = XDocument.Load(@"test.xml");
XNamespace ns3 = "http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4";

string transactionId = doc.Descendants(ns3 + "TransactionID").Single().Value;
string mm7Version = doc.Descendants(ns3 + "MM7Version").Single().Value;
//...select the other elements

TestClass testClass = new TestClass() { TransactionId = transactionId, 
                                        Mm7Version = mm7Version};
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, BG. That is what I was looking for - as you said, I am not filling a List<> of objects, but rather a singular one. 2 things are key about your answer: Not using a LINQ statement with a "from" and the use of the "Single" extension, which I'd never been exposed to. Thank you so much. I'd learned a lot!
I also wanted to add that it looks like I will be using "SingleOrDefault" instead of "Single", as a few of these fields are optional.
@D-Sect yes - if they are optional use SingleOrDefault()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.