开发者

Parsing xml using XDocument, solr results

I want to take the solr response xml, and load a custom collection.

I'd like this to be done the XDocument way, as I need to learn linq to xml.

So the XML looks like:

<response>

<lst>
</list>

<result ... >

<doc>

     <arr name="cat">
     </arr>

     <str name="t1">text</str>
     <str name="t2">text2</str>
     <str name="t3">text3</str>
     <float name="amount">35.99</float>


</doc>
<doc>
..
</doc>

</result&g开发者_开发问答t;

</respones>

My collection:

List<Result> results = new List<Result>();


public class Result
{
   string T1
   string T2
   string T3
   decimal Amount

}


XDocument doc = XDocument.Load(yourXmlfilePath);

List<Result> results = doc.Root.Descendants("doc")
    .Select(e=>new Result
     {
           T1= e.Elements("str").First(s=>s.Attribute("name").Value.Equals("T1")).Value,
           T2= e.Elements("str").First(s=>s.Attribute("name").Value.Equals("T2")).Value,
           T3= e.Elements("str").First(s=>s.Attribute("name").Value.Equals("T3")).Value,
           Amount= decimal.Parse(e.Element("float").First(s=>s.Attribute("name").Value.Equals("amount")).Value)
      }).ToList();


I would take a look at "XML Serialization in the .NET Framework", as it should guide you on how to map the XML from Solr back to the object representation you want.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜