2014-01-14 64 views
0

我想解析XML到一個類,但是我沒有得到我想要的結果。 讓我解釋一下:將c#類序列化爲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; } 
} 

這是實際的結果是:

<?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> 

對於se rialize我使用:

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

我做錯了什麼?!?!?你能指出我嗎?

在此先感謝

回答

1

如果您希望您所需的輸出,你需要你的List改變列表字符串:

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

成爲

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

您正在獲取額外的XML節點,因爲File屬性位於另一個類中。

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; } 
} 
0

如果你不願意放棄最初的結構,並按照@ Pheonixblade9的答案,那麼你可以隨時做一些破解,然後繼續使用EFile類。返回布爾值的ShouldSerialize {}方法告訴序列化程序如何處理相應的屬性,在這種情況下,我們想要做其他一些事情,比如將文件的內容複製到FilesAsString中的新列表中。然後使用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>