9

我有 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>

谁能帮助我最好的方法是什么?

4

2 回答 2

18

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

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);

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

于 2013-02-19T09:16:08.703 回答
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;
        }
    }
}
于 2021-05-01T05:18:32.743 回答