我建議你使用太多直接的方法,爲您的數據是這樣的:
[Serializable()]
public class NodeList {
[XmlArray("Node")]
[XmlArrayItem("DataNode")]
public Test[] Nodes;
}
public class Test {
[XmlAttribute]
public string Name { get; set; }
public int Age { get; set; }
}
並使用它像這樣:
string folderpath = Application.StartupPath + "\\settings";
string appSettingsFilename = "testsettings2";
if (!Directory.Exists(folderpath))
Directory.CreateDirectory(folderpath);
string filepath = folderpath + "\\" + appSettingsFilename + ".xml";
NodeList nodes = new NodeList();
XmlSerializer serializer = new XmlSerializer(typeof(NodeList));
TextWriter configWriteFileStream = new StreamWriter(filepath);
nodes.Nodes = new Test[2] {
new Test() { Name = "Tom", Age=30},
new Test() { Name = "John", Age=35}
};
serializer.Serialize(configWriteFileStream, nodes);
configWriteFileStream.Close();
;你要得到:
<?xml version="1.0" encoding="utf-8"?>
<NodeList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Node>
<DataNode Name="Tom" Age="30" />
<DataNode Name="John" Age="35" />
</Node>
</NodeList>
話雖這麼說,讓XML文件,只要你想,你實際上應該聲明你的類像這樣(註釋):
[Serializable()]
public class DummyClass2 {
[XmlElement("NodeList")] //necessary to indicate that this is an element, otherwise will be considered as array
public TestList[] NodeList = null;
}
public class TestList {
[XmlArray("Node")] //let this be array
[XmlArrayItem("DataNode")]
public Test[] TestItem { get; set; }
}
public class Test {
private string key;
[XmlAttribute("Key")]
public string Key { //declare as Key instead
get { return key; }
set { key = value; }
}
private string value2; //cannot be int, must be string to accommodate both "Tom" and "30"
[XmlAttribute("Value")]
public string Value { //declare as Value instead
get { return value2; }
set { value2 = value; }
}
}
你可以這樣使用它:
string folderpath = Application.StartupPath + "\\settings";
string appSettingsFilename = "testsettings";
if (!Directory.Exists(folderpath))
Directory.CreateDirectory(folderpath);
string filepath = folderpath + "\\" + appSettingsFilename + ".xml";
DummyClass2 dummyClass2 = new DummyClass2();
XmlSerializer serializer = new XmlSerializer(typeof(DummyClass2));
TextWriter configWriteFileStream = new StreamWriter(filepath);
dummyClass2.NodeList = new TestList[2] {
new TestList() {
TestItem = new Test[2] {
new Test() { Key="Name", Value="Tom"},
new Test() { Key="Age", Value="30"}
}
},
new TestList() {
TestItem = new Test[2] {
new Test() { Key="Name", Value="John"},
new Test() { Key="Age", Value="35"}
}
}
};
serializer.Serialize(configWriteFileStream, dummyClass2);
configWriteFileStream.Close();
你應該得到:
<?xml version="1.0" encoding="utf-8"?>
<DummyClass2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<NodeList>
<Node>
<DataNode Key="Name" Value="Tom" />
<DataNode Key="Age" Value="30" />
</Node>
</NodeList>
<NodeList>
<Node>
<DataNode Key="Name" Value="John" />
<DataNode Key="Age" Value="35" />
</Node>
</NodeList>
</DummyClass2>
來源
2016-04-20 07:52:44
Ian
你的代碼在哪裏做序列化? – auburg
你得到的是什麼XML? –
你好,謝謝你的回覆。更新。 – Allen4Tech