简体   繁体   English

使用 C# 和 Serialize 类序列化 XML

[英]Serializing an XML with C# and Serialize class

I am trying to serialize this XML and I have majority of it done, but I'd like to categorize the information correctly, here is my current output.我正在尝试序列化这个 XML 并且我已经完成了大部分工作,但是我想正确地对信息进行分类,这是我当前的输出。

<CountryData>
    <CountryName Type="Text">United States of America</CountryName>
    <CountryName Type="Text">Kingdom of Belgium</CountryName>
    <CountryCode Type="Text">US</CountryCode >
    <CountryCode Type="Text">BE</CountryCode >
    <PercentOfBusiness Type="Numeric">0.5</PercentOfBusiness>
    <PercentOfBusiness Type="Numeric">0.5</PercentOfBusiness>
    <AverageBusiness Type="Numeric">3.5%</AverageBusiness >
    <AverageBusiness Type="Numeric">3.5%</AverageBusiness >
    <SalesMade Type="Text">Very low </SalesMade >
    <SalesMade Type="Text">Low</SalesMade >
</CountryData>

But I would like it like this但我想要这样

<CountryData>
    <Country>
         <CountryName Type="Text">United States of America</CountryName>              
         <CountryCode Type="Text">US</CountryCode >         
         <PercentOfBusiness Type="Numeric">0.5</PercentOfBusiness>
         <AverageBusiness Type="Numeric">3.5</AverageBusiness >
         <SalesMade Type="Text">Very low</SalesMade >
    </Country>
    <Country>
         <CountryName Type="Text">Kingdom of Belgium</CountryName>
         <CountryCode Type="Text">BE</CountryCode >
         <PercentOfBusiness Type="Numeric">0.5</PercentOfBusiness>
         <AverageBusiness Type="Numeric">3.5</AverageBusiness >
         <SalesMade Type="Text">low</SalesMade >
     </Country>
</CountryData>

Here is what I have这是我所拥有的

public class CountryData
{
    #region Public Members
    [XmlElement]
    public List<string> CountryName = new List<string>();
    [XmlElement]
    public List<string> Countrycode= new List<string>();
    [XmlElement]
    public List<string> PercentOfBusiness = new List<string>();
    [XmlElement]
    public List<string> AverageBusiness= new List<string>();
    [XmlElement]
    public List<string> SalesMade= new List<string>();

    ...
}

Then I set those variables to w/e values I have from my feed and serialize然后我将这些变量设置为从我的提要中获得的 w/e 值并序列化

XmlSerializer serializer = new XmlSerializer(this.GetType());
XmlWriterSettings settings = new XmlWriterSettings();
StringBuilder sb = new StringBuilder();
XmlWriter writer;

settings.Indent = true;
settings.IndentChars = "  ";
settings.OmitXmlDeclaration = true;

writer = XmlWriter.Create(sb, settings);
serializer.Serialize(writer, CountryData);
writer.Close();
return sb.ToString();

Would I need to change the way that I instantiate my serialize object?我是否需要更改实例化序列化对象的方式? Also would it be possible to remove the Data type information for each node (eg CountryName Type = "Text")也可以删除每个节点的数据类型信息(例如 CountryName Type = "Text")

Thanks.谢谢。

You want to have CountryData class to contain list of countries, like so:您希望CountryData类包含国家/地区列表,如下所示:

public class CountryData
{
    public List<Country> Countries { get; set; }

    public CountryData() { Countries = new List<Country>(); }
}

public class Country
{
    [XmlElement]
    public string CountryName { get; set; }
    [XmlElement]
    public string Countrycode { get; set; }
    [XmlElement]
    public string PercentOfBusiness { get; set; }
    [XmlElement]
    public string AverageBusiness { get; set; }
    [XmlElement]
    public string SalesMade { get; set; }
}

It will produce a little different xml then the one you asked for but it makes sense:它会产生与您要求的 xml 稍有不同的 xml,但这是有道理的:

<?xml version="1.0" encoding="utf-8"?>
    <CountryData
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <Countries>
            <Country>
                <CountryName>name</CountryName>
                <Countrycode>code</Countrycode>
                <PercentOfBusiness>12</PercentOfBusiness>
                <AverageBusiness>123</AverageBusiness>
                <SalesMade>120012</SalesMade>
           </Country>
           <Country>
                <CountryName>name2</CountryName>
                <Countrycode>code2</Countrycode>
                <PercentOfBusiness>34</PercentOfBusiness>
                <AverageBusiness>345</AverageBusiness>
                <SalesMade>453453543</SalesMade>
           </Country>
    </Countries>
</CountryData>

Main method主要方法

public class Program
{
    public static void Main(string[] args)
    {
        XmlSerializer xsSubmit = new XmlSerializer(typeof(CountryData));
        var subReq = new CountryData();
        subReq.Countries.Add(new Country
        {
            CountryName = "name",
            Countrycode = "code",
            PercentOfBusiness = "12",
            AverageBusiness = "123",
            SalesMade = "120012"
        });
        subReq.Countries.Add(new Country
        {
            CountryName = "name2",
            Countrycode = "code2",
            PercentOfBusiness = "34",
            AverageBusiness = "345",
            SalesMade = "453453543"
        });
        var xml = "";

        using (var sww = new StringWriter())
        {
            using (XmlWriter writer = XmlWriter.Create(sww))
            {
                xsSubmit.Serialize(writer, subReq);
                xml = sww.ToString();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM