【问题标题】:Parsing xml using XDocument, solr results使用 XDocument 解析 xml,solr 结果
【发布时间】:2011-01-22 21:54:12
【问题描述】:

我想获取 solr 响应 xml,并加载自定义集合。

我希望以 XDocument 方式完成,因为我需要学习 linq to xml。

所以 XML 看起来像:

<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>

</respones>

我的收藏:

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


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

}

【问题讨论】:

    标签: c# asp.net xml linq


    【解决方案1】:
    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();
    

    【讨论】:

      【解决方案2】:

      我会看看"XML Serialization in the .NET Framework",因为它应该会指导您如何将 XML 从 Solr 映射回您想要的对象表示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多