如何将List <t>序列化为XML文档。

时间:2015-07-22 15:09:10

标签: c# xml serialization

我对XML序列化没有经验。我需要将System.Collections.Generic.List序列化为XML文档。我有以下课程:

public class Person
{
    public string Name;
    public string Phone;
    public Int32 Score;
}

[Serializable()]
public class PersonOperation:Person
{
    public String City;
    public String OperationType;
    public DateTime OperationDate;
    public Decimal Amount;
    public Decimal AmountEUR;

    public void RoubleToEuro(CurrencyCode p_CurrencyCode, Decimal p_CurrencyRate)
    {
        if (p_CurrencyCode == CurrencyCode.Euro)
        {
            this.AmountEUR = Decimal.Round(Amount / p_CurrencyRate, 2);
        }
    }
}

我有一组PersonOperation实例,我必须序列化为XML。

private List<PersonOperation> _depositorsOperationsList = new List<PersonOperation>();

对于XML序列化,我尝试使用以下方法:

public XmlDocument GetEntityXml<T>()
    {
        XmlAttributeOverrides overrides = new XmlAttributeOverrides();
        XmlAttributes attr = new XmlAttributes();
        attr.XmlRoot = new XmlRootAttribute("Operation");
        overrides.Add(typeof(List<T>), attr);

        XmlDocument xmlDoc = new XmlDocument();
        XPathNavigator nav = xmlDoc.CreateNavigator();
        using (XmlWriter writer = nav.AppendChild())
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<T>), overrides);
            ser.Serialize(writer, _depositorsOperationsList);
        }
        return xmlDoc;
    }

序列化后我需要以下XML格式:

<?xml version="1.0" encoding="Windows-1251" ?>
<Operation>
<PersonOperation>
    <Name>John Smith</Name>
    <Phone>79161234586</Phone>
    <City>Glasgow</City>    
    <Date>2014-02-03</Date>
    <OperationType>Join</OperationType>
    <Amount>9000.00</Amount>
    <AmountEUR>144.06</AmountEUR>
</PersonOperation>
<PersonOperation>
    <Name>Bill Satly</Name>
    <Phone>79163214569</Phone>
    <City>London</City>
    <Date>2014-07-10</Date>
    <OperationType>Join</OperationType>
    <Amount>9000.00</Amount>
    <AmountEUR>144.06</AmountEUR>
</PersonOperation>
. . . . . . . . . . .
<Operation>

但不是这种格式,我有以下单行恐怖:

<Operation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PersonOperation><Name>John Smith</Name><Phone>79161234586</Phone><Score>270</Score><City>Glasgow</City><OperationType>Join</OperationType><OperationDate>2014-02-03</OperationDate><Amount>9000.0000</Amount><AmountEUR>144.06</AmountEUR></PersonOperation><PersonOperation><Name>Bill Satly</Name><Phone>79163214569</Phone><Score>270</Score><City>London</City><OperationType>Join</OperationType><OperationDate>2014-07-10</OperationDate><Amount>9000.0000</Amount><AmountEUR>144.06</AmountEUR></PersonOperation></Operation>

如何修复我的GetEntityXml方法以获得正确的XML格式?

3 个答案:

答案 0 :(得分:1)

XmlWriter的{​​{1}}属性类型为Settings。 尝试使用它来指定要使用的格式。

XmlWriterSettings

更多信息: https://msdn.microsoft.com/en-us/library/kbef2xz3(VS.80).aspx

答案 1 :(得分:0)

从我的代码中粘贴片段,效果很好..

Public Function ToXmlString() As String
    Dim builder = New StringBuilder()
    Dim xmlSerializer = New XmlSerializer(GetType(List(Of T)), New XmlRootAttribute(_rootNodeName))
    Using writer = XmlWriter.Create(builder, New XmlWriterSettings() With {.OmitXmlDeclaration = True})
        xmlSerializer.Serialize(writer, _source)
    End Using
    Return builder.ToString()
End Function

----转换C#代码----

public string ToXmlString()
{
    var builder = new StringBuilder();
    var xmlSerializer = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(_rootNodeName));
    using (writer = XmlWriter.Create(builder, new XmlWriterSettings { OmitXmlDeclaration = true })) {
        xmlSerializer.Serialize(writer, _source);
    }
    return builder.ToString();
}

其中_rootNodeName是xml根节点的名称,_source是sourceList

然后从字符串创建XMLDoc -

XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlString); 

答案 2 :(得分:0)

如果您使用特殊的XML编辑器打开文件,或者只使用Chrome或Internet Explorer等网络导航器打开文件,它将显示为您想要的内容。