将c#类序列化为xml

时间:2014-01-14 18:07:49

标签: c# xml

我正在尝试向xml解析一个类,但是我没有得到我想要的结果。 让我解释一下:

这些是以下类:

public class ParCard
{
    public string ExtrFreq { get; set; }
    public string LastDay { get; set; }
    public string FolderPath { get; set; }
    public List<EFile> Files { get; set; }
    //public List<string> Files { get; set; }
    public string FTPAddress { get; set; }
    public string FTPPath { get; set; }
    public string FTPUser { get; set; }
    public string FTPPass { get; set; }
}

public class EFile
{
    public string FileName { get; set; }
}

public class ParCard { public string ExtrFreq { get; set; } public string LastDay { get; set; } public string FolderPath { get; set; } public List<EFile> Files { get; set; } //public List<string> Files { get; set; } public string FTPAddress { get; set; } public string FTPPath { get; set; } public string FTPUser { get; set; } public string FTPPass { get; set; } } public class EFile { public string FileName { get; set; } }

这是实际结果:

这就是我想要实现的目标:

<?xml version="1.0" encoding="utf-8"?>
<ParCard xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ExtrFreq>Daily</ExtrFreq>
<LastDay>20140101</LastDay>
<FolderPath>c:\Temp\</FolderPath>
<Files>
  <EFile>
    <FileName>file1.txt</FileName>
  </EFile>
  <EFile>
    <FileName>file2.txt</FileName>
  </EFile>
  <EFile>
    <FileName>file3.txt</FileName>
  </EFile>
</Files>
<FTPAddress>10.1.1.100</FTPAddress>
<FTPPath>Home</FTPPath>
<FTPUser>User</FTPUser>
<FTPPass>Pass</FTPPass>
</ParCard>

对于序列化我使用: <?xml version="1.0" encoding="utf-8"?> <ParCard xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ExtrFreq>Daily</ExtrFreq> <LastDay>20140101</LastDay> <FolderPath>c:\Temp\</FolderPath> <Files> <FileName>file1.txt</FileName> <FileName>file2.txt</FileName> <FileName>file3.txt</FileName> </Files> <FTPAddress>10.1.1.100</FTPAddress> <FTPPath>Home</FTPPath> <FTPUser>User</FTPUser> <FTPPass>Pass</FTPPass>

static public void Serialize(ParCard pc)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(ParCard));
        using (TextWriter writer = new StreamWriter(@"Teste.xml"))
        {
            serializer.Serialize(writer, pc);
        }
    }

我做错了什么?!?!?你能指出我吗?

提前致谢

3 个答案:

答案 0 :(得分:2)

我不确定是否有更好的方法,但这有效:

public class ParCard
{
    public string ExtrFreq { get; set; }
    public string LastDay { get; set; }
    public string FolderPath { get; set; }
    public EFile Files { get; set; }
    public string FTPAddress { get; set; }
    public string FTPPath { get; set; }
    public string FTPUser { get; set; }
    public string FTPPass { get; set; }
}

public class EFile
{
    [XmlElement("FileName")]
    public List<string> FileName { get; set; }
}

答案 1 :(得分:1)

如果您需要所需的输出,则需要将List更改为字符串列表:

public List<EFile> Files { get; set; }

变为

public List<string> Files { get; set; }

您正在获取额外的XML节点,因为File属性位于另一个类中。

答案 2 :(得分:0)

如果您不愿意放弃初始结构,并按照@ Pheonixblade9的回答,那么您仍然可以随意使用它来获取它,同时仍然使用EFile类。返回bool的ShouldSerialize {}方法告诉序列化程序如何处理相应的属性,在这种情况下我们还想做一些其他的东西,比如将Files的内容复制到FilesAsString中的新List中。然后使用XmlIgnore忽略原始的Files属性,然后使用XmlArray和XmlArrayItem重新定义集合上的名称以及元素。

class Program
{
    static void Main(string[] args)
    {

        ParCard pc = new ParCard();
        pc.ExtrFreq = "Daily";
        pc.LastDay = "20140101";
        pc.FolderPath = @"c:\temp";
        pc.Files = new List<EFile>() { new EFile() { FileName = "file1.txt" }, new EFile { FileName = "file2.txt" } };
        pc.FTPAddress = "10.1.1.100";
        pc.FTPPath = "Home";
        pc.FTPUser = "User";
        pc.FTPPass = "Pass";

        Serialize(pc);
    }

    static public void Serialize(ParCard pc)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(ParCard));
        using (TextWriter writer = new StreamWriter(@"Teste.xml"))
        {
            serializer.Serialize(writer, pc);
        }
    }
}

public class ParCard
{
    public string ExtrFreq { get; set; }
    public string LastDay { get; set; }
    public string FolderPath { get; set; } 

    [XmlIgnore]
    public List<EFile> Files { get; set; }

    [XmlArray("Files"), XmlArrayItem(typeof(string), ElementName = "FileName")]
    public List<string> FilesAsString { get; set; }
    public string FTPAddress { get; set; }
    public string FTPPath { get; set; }
    public string FTPUser { get; set; }
    public string FTPPass { get; set; }

    public bool ShouldSerializeFilesAsString()
    {
        List<string> fileNames = new List<string>();
        foreach (var eFile in Files)
        {
            fileNames.Add(eFile.FileName);
        }

        FilesAsString = fileNames;
        return true;
    }
}


public class EFile
{
    public string FileName { get; set; }
}

这将产生您想要的输出:

<?xml version="1.0" encoding="utf-8"?>
<ParCard xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ExtrFreq>Daily</ExtrFreq>
    <LastDay>20140101</LastDay>
    <FolderPath>c:\temp</FolderPath>
    <Files>
        <FileName>file1.txt</FileName>
        <FileName>file2.txt</FileName>
    </Files>
    <FTPAddress>10.1.1.100</FTPAddress>
    <FTPPath>Home</FTPPath>
    <FTPUser>User</FTPUser>
    <FTPPass>Pass</FTPPass>
</ParCard>