开发者

How to Load XML Into Workable Object From Fragment String?

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>


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


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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜