2012-11-21 40 views
3

我已經在這個網上搜索了linq解決方案,但是我找不到任何相當類似的解決方案。從xml中選擇作爲LINQ中的記錄

誰能告訴我如何從xml中選擇構件元素?我想在c#中使用linq將每個組件(id,accountnumber,type)添加到一個對象中。

其實我想一次獲得一堆/記錄中的數據,然後將它添加到列表中。我究竟做錯了什麼?

XML

string batch = @" 
<batchresult> 
     <outputs> 
    <output> 
     <value> 
     <conformation> 
      <component> 
      <name>cmdinfo</name> 
      <value> 
       <bundle> 
       <data> 
        <value> 
        <state>100</state> 
        </value> 
       </data> 
       </bundle> 
      </value> 
      </component> 
      <component> 
      <name>resultlist</name> 
      <value> 
       <bundle> 
       <data> 
        <value> 
        <conformation> 
         <component> 
         <name>id</name> 
         <value> 
          <state>1</state> 
         </value> 
         </component> 
         <component> 
         <name>accountnumber</name> 
         <value> 
          <string>10505749</string> 
         </value> 
         </component> 
         <component> 
         <name>type</name> 
         <value> 
          <string>subnumber</string> 
         </value> 
         </component> 
        </conformation> 
        </value> 
        <value> 
        <conformation> 
         <component> 
         <name>id</name> 
         <value> 
          <state>2</state> 
         </value> 
         </component> 
         <component> 
         <name>accountnumber</name> 
         <value> 
          <string>53871265</string> 
         </value> 
         </component> 
         <component> 
         <name>type</name> 
         <value> 
          <string>subnumber</string> 
         </value> 
         </component> 
        </conformation> 
        </value> 
       </data> 
       </bundle> 
      </value> 
      </component> 
      <component> 
      <name>batchnumber</name> 
      <value> 
       <bundle> 
       <data> 
        <value> 
        <state>14512367</state> 
        </value> 
       </data> 
       </bundle> 
      </value> 
      </component> 
     </conformation> 
     </value> 
    </output> 
    </outputs> 
    </batchresult>"; 

CODE

 class record 
     { 
      string F1 = ""; 
      public string f1 
      { 
       set { F1 = value; } 
       get { return F1; } 
      } 

      string F2 = ""; 
      public string f2 
      { 
       set { F2 = value; } 
       get { return F2; } 
      } 

      string F3 = ""; 
      public string f3 
      { 
       set { F3 = value; } 
       get { return F3; } 
      } 
     } 


    private void button1_Click(object sender, EventArgs e) 
    { 
      XDocument xmldoc = XDocument.Parse(batch); 
      var obj = from o in xmldoc.Descendants("component") 
         where o.Parent.Name == "conformation" 
    //select new record { f1 = o.Element("name").Value, f2 = o.Element("value").Value, f3 = o.Element("value").Value }; 

         select new record { f1 = o.Element("id").Value, f2 = o.Element("accountnumber").Value, f3 = o.Element("type").Value }; 
      foreach (var d in obj) 
      { 
    record myobj = new record(); 
      myobj.f1=d.f1;//the first object will contain 1 
      myobj.f2=d.f2; //10505749 
      myobj.f3=d.f3;//subnumber .. and so on 
      mylist.Add(myobj); 
      } 
    } 

嗨,我還沒有嘗試反序列化,我會檢查出來。這是從你一貼出這段代碼的例子:

XElement doc = XElement.Parse(batch); 
doc.Descendants("conformation").Elements("component").Select(x => 
new record 
{ 
    f1 = x.First().Element("name").Value, 
    f2 = x.Skip(1).First().Element("name").Value, 
    f3 = x.Skip(2).Element("name").Value 
}; 

..是給我下面的錯誤:

錯誤15「System.Xml.Linq.XElement」不包含一個定義'First'並且沒有擴展方法'First'接受類型'System.Xml.Linq.XElement'的第一個參數可以被找到(你是否缺少using指令或者程序集引用?)

怎麼回事?

+0

您能得到什麼?你可以只發布XML格式,而不是整個文件? – elyashiv

+0

而不是LINQ來創建一個對象爲什麼不將'XML反序列化到一個對象? – LukeHennerley

+0

你的XML不是'正確'... – Anirudha

回答

0

試試這個

XDocument doc = XDocument.Parse(batch); 
var result = doc. 
    XPathSelectElements("//component[name[text() = 'resultlist']]"). 
    Descendants("conformation"). 
    Select(y => new record 
     { 
      f1 = (string)y.XPathSelectElement("component[name[text() = 'id']]/value/state"), 
      f2 = (string)y.XPathSelectElement("component[name[text() = 'accountnumber']]/value/string"), 
      f3 = (string)y.XPathSelectElement("component[name[text() = 'type']]/value/string") 
     } 
    ); 

而不使用XPath:

var result = doc. 
    Descendants("conformation"). 
    Select(x => x.Elements("component")). 
    Select(y => y.Select(z => new { name = (string)z.Element("name"), value = z.Element("value") })). 
    Where(z => z.Any(m => m.name == "id")). 
    Select(a => new record 
     { 
      f1 = (string)a.Where(m => m.name == "id").FirstOrDefault().value.Element("state"), 
      f2 = (string)a.Where(m => m.name == "accountnumber").FirstOrDefault().value.Element("string"), 
      f3 = (string)a.Where(m => m.name == "type").FirstOrDefault().value.Element("string") 
     } 
    );