How do I load XML into a work object from a fragmentary string?

I have an XML framing (just a few nodes, not a full XML document) in a line where I need to get some values ​​from Linq to XML. I'm having a hard time getting this string in an XML object that I can work with though.

Is there a way to do this?

This is a sample 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>

      

+2


a source to share


3 answers


xml is intact, so any xml model should work. XmlSerializer

must be accurate, for example; but what about the 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 study batch

;-p

+2


a source


You must do this. If there are multiple package elements and you've only included one in your fragment, you need to wrap the entire fragment in the root, perhaps <Batches> {xml} </Batches>

so that you can work with it. If so where am I talking

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

      

Change this to



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

      

the 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") }
                                }
            };

      

+1


a source


As long as you have whole nodes, you can just wrap the string in one element tag and load it.

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

0


a source







All Articles