我寧願寫一個數據類是這樣的:
[Serializable]
[XmlRoot("acc")]
public class Account
{
[XmlElement("Account")]
public string Account { get; set; }
[XmlElement("Partner")]
public int Partner { get; set; }
[XmlElement("CITY")]
public string City { get; set; }
}
和序列化/與XmlSerializer
反序列化。
List<AccountClass> accounts = new List<AccountClass>
{
new AccountClass { Account = "test", Partner = 2144, City = "Munsbach" },
new AccountClass { Account = "test", Partner = 2144, City = "Schuttrange" }
};
XmlSerializer ser = new XmlSerializer(typeof(List<AccountClass>));
using (FileStream fileStream = new FileStream("File.xml", FileMode.OpenOrCreate))
{
ser.Serialize(fileStream, accounts);
}
您的文件將是這樣的:
<?xml version="1.0"?>
<ArrayOfAccountClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AccountClass>
<Account>test</Account>
<Partner>2144</Partner>
<CITY>Munsbach</CITY>
</AccountClass>
<AccountClass>
<Account>test</Account>
<Partner>2144</Partner>
<CITY>Schuttrange</CITY>
</AccountClass>
</ArrayOfAccountClass>
我希望該文件也有一個根元素 - 你已經證明是無效的XML文檔。 –
也看到這個問題http://stackoverflow.com/questions/2948255/xml-file-creation-using-xdocument-in-c-sharp – stombeur