将XDocument元素提取到类对象 [英] Extract XDocument elements to class object

查看:133
本文介绍了将XDocument元素提取到类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从XDocument中提取元素并将其存储在类对象中。



当我尝试Descendants和Elements函数时,我得到空结果,下面是我得到的xml结构



Hi, I am trying to extract elements from XDocument and store it in class object.

I am getting empty result when I try Descendants and Elements functions, below is the structure of xml I get

<getformsresponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://services.sample.com/forms">
  <eventid>00000000-0000-0000-0000-000000000000</eventid>
  <statuscode>0</statuscode>
  <statusmessage i:nil="true" />
  <forms>
    <form>
      <description>Application</description>
      <edition>2013/01</edition>
      <effectivedate>01/01/2013</effectivedate>
      <gid>00000000-0000-0000-0000-000000000000</gid>
      <linesofbusiness>
        <lineofbusiness>WORK</lineofbusiness>
      </linesofbusiness>
      <mappingenabled>true</mappingenabled>
      <name>130</name>
      <states i:nil="true" />
    </form>
    <form>
      <description>Property</description>
      <edition>2011/10</edition>
      <effectivedate>10/01/2011</effectivedate>
      <gid>00000000-0000-0000-0000-000000000000</gid>
      <linesofbusiness>
        <lineofbusiness>PROPC</lineofbusiness>
      </linesofbusiness>
      <mappingenabled>true</mappingenabled>
      <name>140</name>
      <states i:nil="true" />
    </form>
    <form>
      <description>Commercial Application</description>
      <edition>2013/09</edition>
      <effectivedate>09/01/2013</effectivedate>
      <gid>00000000-0000-0000-0000-000000000000</gid>
      <linesofbusiness>
        <lineofbusiness>AAPPL</lineofbusiness>
        <lineofbusiness>ACCT</lineofbusiness>
      </linesofbusiness>
      <mappingenabled>true</mappingenabled>
      <name>125</name>
      <states i:nil="true" />
    </form>
</getformsresponse>







我创建的课程如下






I created class like below

public class GetFormsListResponse
{
    public string Name { get; set; }

    public string Gid { get; set; }

    public string EffectiveDate { get; set; }

    public string Edition { get; set; }

    public string Description { get; set; }

    public StatesResponse States { get; set; }

    public LobResponse LineOfBusiness { get; set; }
}

public class GetFormsResponseCollection : List<GetFormsListResponse>
{

}

public class LobResponse : List<string>
{

}

public class StatesResponse : List<string>
{

}







并尝试将提取的结果放入集合中。




and trying to put the extracted result into the collection.

推荐答案

好的,这里的问题是你指定了一个默认的命名空间(错过了)



请尝试以下代码:

Okay, the issue here is you have specified a default namespace (missed that)

Try the following code:
XDocument doc = XDocument.Parse(response);

XNamespace ns = "http://services.sample.com/forms";
IEnumerable<xelement> descendants = doc.Descendants(ns + "form"); // An alternative notation would be doc.Descendants("{http://services.sample.com/forms}forms")
foreach (XElement d in descendants)
{
    // process each form element.
}
</xelement>





在您的示例XML中,最后一个表单标签丢失,我认为情况并非如此实际响应?



In your example XML the last forms tag is missing, I assume that's not the case with the actual response?


XmlDocument doc = new XmlDocument();
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", "http://services.sample.com/eforms");
doc.Load(@"C:\\Response.xml");
XmlNode formsNode = doc.SelectSingleNode("/x:getformsresponse/x:forms", nsmgr);
XmlNodeList formNodeList = formsNode.SelectNodes("x:form", nsmgr);

GetFormsResponse responseList = new GetFormsResponse();
foreach (XmlNode i in formNodeList)
{
  GetFormsData data = new GetFormsData();

  data.Description = i["description"].InnerText;
  data.Edition = i["edition"].InnerText;
  data.EffectiveDate = i["effectivedate"].InnerText;
  data.Gid = i["gid"].InnerText;
  data.Name = i["name"].InnerText;

  foreach (XmlNode l in i["linesofbusiness"])
  {
    LineOfBusiness buss = new LineOfBusiness();
    buss.Add(l.InnerText);
    data.LineOfBusiness = buss;
  }

  foreach (XmlNode l in i["states"])
  {
    States state = new States();
    state.Add(l.InnerText);
    data.States = state;
  }

  responseList.Add(data);
}


这篇关于将XDocument元素提取到类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆