根据我的c#类生成xml文件

时间:2013-02-19 08:51:13

标签: c# xml c#-4.0 xml-serialization

我有xml文件,我需要根据新的客户端要求每次更新。 大多数情况下,由于手动更新xml文件,xml不正确。 我正在考虑编写一个程序(web / windows),提供适当的验证。 并根据ui的输入,我将创建xml文件。 下面是我的示例xml文件。

<community>
  <author>xxx xxx</author>
  <communityid>000</communityid>
  <name>name of the community</name>

<addresses>
        <registeredaddress>
          <addressline1>xxx</addressline1>
          <addressline2>xxx</addressline2>
          <addressline3>xxx</addressline3>
          <city>xxx</city>
          <county>xx</county>
          <postcode>0000-000</postcode>
          <country>xxx</country>
        </registeredaddress>
        <tradingaddress>
          <addressline1>xxx</addressline1>
          <addressline2>xxx</addressline2>
          <addressline3>xxx</addressline3>
          <city>xxx</city>
          <county>xx</county>
          <postcode>0000-000</postcode>
          <country>xxx</country>
        </tradingaddress>
      </addresses>


<community>

任何人都可以帮助我最好的方法吗?

2 个答案:

答案 0 :(得分:14)

创建以下类来保存数据并对其进行验证:

public class Community
{
    public string Author { get; set; }
    public int CommunityId { get; set; }
    public string Name { get; set; }
    [XmlArray]
    [XmlArrayItem(typeof(RegisteredAddress))]
    [XmlArrayItem(typeof(TradingAddress))]
    public List<Address> Addresses { get; set; }
}

public class Address
{
    private string _postCode;

    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string City { get; set; }
    public string Country { get; set; }

    public string PostCode
    {
        get { return _postCode; }
        set {
            // validate post code e.g. with RegEx
            _postCode = value; 
        }
    }
}

public class RegisteredAddress : Address { }
public class TradingAddress : Address { }

将社区类的实例序列化为xml:

Community community = new Community {
    Author = "xxx xxx",
    CommunityId = 0,
    Name = "name of community",
    Addresses = new List<Address> {
        new RegisteredAddress {
            AddressLine1 = "xxx",
            AddressLine2 = "xxx",
            AddressLine3 = "xxx",
            City = "xx",
            Country = "xxxx",
            PostCode = "0000-00"
        },
        new TradingAddress {
            AddressLine1 = "zz",
            AddressLine2 = "xxx"
        }
    }
};

XmlSerializer serializer = new XmlSerializer(typeof(Community));
serializer.Serialize(File.Create("file.xml"), community);

我认为有点谷歌搜索将帮助您了解如何从文件反序列化社区对象。

答案 1 :(得分:0)

这是一个老话题,但我想分享我自己的观点。 您上面提供的 xml 无效,所以我以下面的 xml 为例。

  1. 首先,按照您希望的方式构建您的 xml。假设它看起来像这样:

    <note>
          <to>Tove</to>
          <from>Jani</from>
          <heading>Reminder</heading>
          <body>Don't forget me this weekend!</body>
    </note>
    
  2. 复制你的xml代码

  3. 转到 Visual Studio

  4. 创建类文件并清除其中的代码

  5. 然后在菜单上,点击 Edit\Paste Special\Paste Xml as Classes

Visual Studio 将生成整个类。

Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class note
{

    private string toField;

    private string fromField;

    private string headingField;

    private string bodyField;

    /// <remarks/>
    public string to
    {
        get
        {
            return this.toField;
        }
        set
        {
            this.toField = value;
        }
    }

    /// <remarks/>
    public string from
    {
        get
        {
            return this.fromField;
        }
        set
        {
            this.fromField = value;
        }
    }

    /// <remarks/>
    public string heading
    {
        get
        {
            return this.headingField;
        }
        set
        {
            this.headingField = value;
        }
    }

    /// <remarks/>
    public string body
    {
        get
        {
            return this.bodyField;
        }
        set
        {
            this.bodyField = value;
        }
    }
}