views:

25

answers:

3

I have a framgment of XML (just a few nodes, not a complete XML document) in a string that I need to get some values out of with Linq to XML. I'm finding it quite difficult to get this string into an XML object that I can work with though.

Is there a way for me to do this?

This is a sample of the XML:

<Batch type="My Funds" pageNumber="69">
  <FundGroup heading="Best Funds Ever">
    <fund id="367"/>
    <fund id="373"/>
    <fund id="378"/>
  </FundGroup>
  <FundGroup heading="Almost Best Funds">
    <fund id="665"/>
    <fund id="666"/>
    <fund id="667"/>
  </FundGroup>
</Batch>
A: 

As long as you have whole nodes you could just wrap the string in a single element tag and load that.

"<xml>" + myFragment + "</xml>";

Adrian
+2  A: 

The xml is intact, so any xml-model should work. XmlSerializer should be fine, for example; but how about an ad-hoc model of anonymous types?

        var doc = XElement.Parse(xml);
        var batch = new {
            Type = (string)doc.Attribute("type"),
            Page = (int)doc.Attribute("pageNumber"),
            Groups = (from fg in doc.Elements("FundGroup")
                      select new {
                          Heading = fg.Attribute("heading"),
                          Funds = (from fund in fg.Elements("fund")
                                   select (int)fund.Attribute("id")).ToList()
                      }).ToList()
        };

Now explore batch ;-p

Marc Gravell
@Mark, this is above and beyond the call of duty - you saved me hours!! (always good at the end of a Friday :-) ) Thanks man. I have to wait another couple of minutes before I check it correct.
DaveDev
+1  A: 

You should be able to do this. If there are multiple batch elements and you only included one in your snippet, you will need to wrap the whole snippet in a root, perhaps <Batches> {xml} </Batches> so you can work with it. If so, where I say

var query = from batch in document.Elements("Batch") ...

Modify that to

... document.Root.Elements("Batch") or document.Descendants("Batch")

Code:

string xml = @"<Batch type=""My Funds"" pageNumber=""69""> 
                    <FundGroup heading=""Best Funds Ever""> 
                    <fund id=""367""/> 
                    <fund id=""373""/> 
                    <fund id=""378""/> 
                    </FundGroup> 
                    <FundGroup heading=""Almost Best Funds""> 
                    <fund id=""665""/> 
                    <fund id=""666""/> 
                    <fund id=""667""/> 
                    </FundGroup> 
                </Batch>";

XDocument document = XDocument.Parse(xml);

var query = from batch in document.Elements("Batch")
            select new
            {
                Type = batch.Attribute("type").Value,
                PageNumber = (int)batch.Attribute("pageNumber"),
                FundGroups = from fundGroup in batch.Elements("FundGroup")
                                select new
                                {
                                    Heading = fundGroup.Attribute("heading").Value,
                                    Funds = from fund in fundGroup.Elements("fund")
                                            select new { Id = (int)fund.Attribute("id") }
                                }
            };
Anthony Pegram
Excellent answer, but Mark supplied his answer first so I had to give that the check. You get a +1 though!! Thanks Anthony :-)
DaveDev
Glad to help, albeit a bit slower!
Anthony Pegram