选择xml作为linq中的记录

时间:2012-11-21 16:12:18

标签: c# xml linq

我已经在网上搜索了linq解决方案,但我找不到任何相似的解决方案。

有谁能告诉我如何从下面的xml中选择comformation元素?我想在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'的第一个参数'(是你错过了使用指令或程序集引用?)

怎么回事?

1 个答案:

答案 0 :(得分: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")
        }
    );