我想解析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);
}
}
我做錯了什麼?!?!?你能指出我嗎?
在此先感謝