【问题标题】:generate xml files based on my c# classes根据我的 c# 类生成 xml 文件
【发布时间】:2013-02-19 08:51:12
【问题描述】:

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

谁能帮我解决这个问题最好的方法是什么?

【问题讨论】:

  • 你在说什么样的验证?
  • 喜欢邮政编码字段正则表达式
  • @Prashant xml 标签小写重要吗?
  • 是的。实际上,这个 xml 是由其他一些遗留工具导入以进行进一步处理的。

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


【解决方案1】:

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

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

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

【讨论】:

  • xml 中的 xml 标签与 cs 文件中的相同。我怎样才能像原来的 xml 文件一样缩小大小写。
  • @Prashant 使用XmlElement 属性。像这个[XmlElement("name")] public string Name { get; set; } 一样,社区类也需要XmlRoot 属性。
  • 你能指定XmlSerializer的命名空间吗,这样就不用google搜索了。
【解决方案2】:

这是一个老话题,但我想分享一下自己的观点。 您上面提供的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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2011-05-11
    相关资源
    最近更新 更多