C# 如何使用自定义模型类从XML构建对象集合?

C# 如何使用自定义模型类从XML构建对象集合?,c#,asp.net-mvc,asp.net-mvc-4,mapping,xml-serialization,C#,Asp.net Mvc,Asp.net Mvc 4,Mapping,Xml Serialization,我有以下xml文档 <class Title="SOCIAL HISTORY" ID="1" ParentID="0"> <helpNavNode Title="ALCOHOL CONSUMPTION" ID="2" ParentID="1"> <helpNavNode Title=" Never" ID="3" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Never" /> <helpN

我有以下
xml
文档

<class Title="SOCIAL HISTORY" ID="1" ParentID="0">
  <helpNavNode Title="ALCOHOL CONSUMPTION" ID="2" ParentID="1">
    <helpNavNode Title=" Never" ID="3" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Never" />
    <helpNavNode Title=" Occasionally" ID="4" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Occasionally" />
    <helpNavNode Title=" Socially" ID="5" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Socially" />
    <helpNavNode Title=" Daily (3 beers/day) (6 pack/day)" ID="6" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Daily (3 beers/day) (6 pack/day)" />
    <helpNavNode Title=" Is Alcoholic" ID="7" ParentID="2" Narrative="ALCOHOL CONSUMPTION: Is Alcoholic" />
    <helpNavNode Title=" History of Alcoholism" ID="8" ParentID="2" Narrative="ALCOHOL CONSUMPTION: History of Alcoholism" />
    <helpNavNode Title=" None" ID="26" ParentID="2" Narrative="ALCOHOL CONSUMPTION: None" />
    <helpNavNode Title="Alcoholism Screen Question" ID="39" ParentID="2">
      <helpNavNode Title=" Ever had a drinking problem? Last drink? " ID="40" ParentID="39" Narrative="ALCOHOL CONSUMPTION:Alcoholism Screen Question: Ever had a drinking problem? Last drink? " />
    </helpNavNode>
  </helpNavNode>
  <helpNavNode Title="MARITAL STATUS" ID="9" ParentID="1">
    <helpNavNode Title=" Married" ID="10" ParentID="9">
      <helpNavNode Title=" x 10 years" ID="15" ParentID="10" Narrative="MARITAL STATUS: Married: x 10 years" />
    </helpNavNode>
    <helpNavNode Title=" Divorced" ID="11" ParentID="9" Narrative="MARITAL STATUS: Divorced" />
    <helpNavNode Title=" Single" ID="12" ParentID="9" Narrative="MARITAL STATUS: Single" />
  </helpNavNode>
  <helpNavNode Title="Recreational drug use" ID="13" ParentID="1" Narrative=":Recreational drug use" />
  <helpNavNode Title="OCCUPATION" ID="14" ParentID="1">
    <helpNavNode Title=" works for  x 27 years" ID="25" ParentID="14" Narrative="OCCUPATION: works for  x 27 years" />
  </helpNavNode>
  <helpNavNode Title=" 1 child" ID="16" ParentID="1" Narrative=": 1 child" />
  <helpNavNode Title=" 2/3/4/5/6 children" ID="17" ParentID="1" Narrative=": 2/3/4/5/6 children" />
  <helpNavNode Title="Activities" ID="18" ParentID="1">
    <helpNavNode Title=" walks regularly" ID="19" ParentID="18" Narrative="Activities: walks regularly" />
    <helpNavNode Title=" cycles for exercise" ID="20" ParentID="18" Narrative="Activities: cycles for exercise" />
    <helpNavNode Title=" runs 3 days/week" ID="21" ParentID="18" Narrative="Activities: runs 3 days/week" />
    <helpNavNode Title=" hunting and fishing" ID="22" ParentID="18" Narrative="Activities: hunting and fishing" />
    <helpNavNode Title=" hiking and camping" ID="23" ParentID="18" Narrative="Activities: hiking and camping" />
    <helpNavNode Title=" swims regularly" ID="24" ParentID="18" Narrative="Activities: swims regularly" />
  </helpNavNode>
  <helpNavNode Title="Tobacco Use" ID="27" ParentID="1">
    <helpNavNode Title="Cigar, Cigarette, Pipe, Smokeless" ID="30" ParentID="27">
      <helpNavNode Title="10/pk year" ID="32" ParentID="30" Narrative="Tobacco Use:Cigar, Cigarette, Pipe, Smokeless:10/pk year" />
    </helpNavNode>
    <helpNavNode Title="Quit Date" ID="35" ParentID="27" Narrative="Tobacco Use:Quit Date" />
    <helpNavNode Title="Year Started" ID="36" ParentID="27" Narrative="Tobacco Use:Year Started" />
  </helpNavNode>
  <helpNavNode Title="SocHx  Template" ID="37" ParentID="1">
    <helpNavNode Title=" Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: " ID="41" ParentID="37" Narrative="SocHx  Template: Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: " />
    <helpNavNode Title=" Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: " ID="42" ParentID="37" Narrative="SocHx  Template: Living situation:  Occupation:  Tobacco:  EtOH:  Rec. drugs: " />
  </helpNavNode>
</class>

我需要将此文档映射到类中

public class @class
{
    public string id { get; set; }
    public string text { get; set; }
    public string parent { get; set; }
    public string value { get; set; }

    public List<helpNavNode> nodes { get; set; }

    public @class()
    {
        nodes = new List<helpNavNode>();
    }
}

    public class helpNavNode
    {
        public string id { get; set; }
        public string text { get; set; }
        public string parent { get; set; }
        public string value { get; set; }
    }
public class@class
{
公共字符串id{get;set;}
公共字符串文本{get;set;}
公共字符串父项{get;set;}
公共字符串值{get;set;}
公共列表节点{get;set;}
public@class()
{
节点=新列表();
}
}
公共类帮助导航节点
{
公共字符串id{get;set;}
公共字符串文本{get;set;}
公共字符串父项{get;set;}
公共字符串值{get;set;}
}
我添加了
id、text、parent、value
,因为父节点
“class”
和子节点
“helpNavNode”
都具有公共属性


节点
'class'
的属性
Title,ID,ParentID
必须映射到
@class
中的
文本,ID,ParentID
,节点
标题,ID,ParentID,叙述
“helpNavNode”
必须映射到
“helpNavNode”
类的
文本,ID,parent,value
。因此,如何创建
“@class”
“helNavNode”
的对象集合?

我建议不要编写自定义序列化程序。所以我建议你们创建一个类,这个类可以很容易地对你们的结构进行理想化。这是:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class @class
{

    private classHelpNavNode[] helpNavNodeField;

    private string titleField;

    private byte idField;

    private byte parentIDField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("helpNavNode")]
    public classHelpNavNode[] helpNavNode
    {
        get
        {
            return this.helpNavNodeField;
        }
        set
        {
            this.helpNavNodeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Title
    {
        get
        {
            return this.titleField;
        }
        set
        {
            this.titleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte ParentID
    {
        get
        {
            return this.parentIDField;
        }
        set
        {
            this.parentIDField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class classHelpNavNode
{

    private classHelpNavNode[] helpNavNodeField;

    private string titleField;

    private byte idField;

    private byte parentIDField;

    private string narrativeField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("helpNavNode")]
    public classHelpNavNode[] helpNavNode
    {
        get
        {
            return this.helpNavNodeField;
        }
        set
        {
            this.helpNavNodeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Title
    {
        get
        {
            return this.titleField;
        }
        set
        {
            this.titleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte ParentID
    {
        get
        {
            return this.parentIDField;
        }
        set
        {
            this.parentIDField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Narrative
    {
        get
        {
            return this.narrativeField;
        }
        set
        {
            this.narrativeField = value;
        }
    }
}

现在,由于您的数据位于
data
对象中。将数据映射到您的结构非常容易。你甚至可以使用类似的东西。

一个好的开始是阅读Linq to XML:你可以使用这个框架来读取XML并将其反序列化到类中。但是我不希望类具有与XML相同的属性,例如:Title属性必须由文本替换(小写“text”)标题必须映射到“文本”的点,依此类推。但我建议您只通过映射而不是通过自定义序列化将这些数据转换为您的数据
@class data = null;
string path = @"D:\test.xml"; //Your xml path

XmlSerializer serializer = new XmlSerializer(typeof(@class));

using (StreamReader reader = new StreamReader(path))
{
    data = (@class)serializer.Deserialize(reader);
}