C# XML序列化的工作方式与预期不同
我得到了一个具有这种结构的C# XML序列化的工作方式与预期不同,c#,xml,xsd,C#,Xml,Xsd,我得到了一个具有这种结构的.xsd文件(我还使用一个VisualStudio工具为它生成了类) 这就是我的方法生成xml的方式 <?xml version="1.0" encoding="utf-8"?> <data_container xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/"> <other_prope
.xsd
文件(我还使用一个VisualStudio工具为它生成了类)
这就是我的方法生成xml的方式
<?xml version="1.0" encoding="utf-8"?>
<data_container xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/">
<other_propertiesField>
<introduction_picField>a</introduction_picField>
<introduction_txtField>b</introduction_txtField>
</other_propertiesField>
<peopleField>
<data_containerPerson>
<p_idField i:nil="true" />
<some_dataField>
<sxField>F</sxField>
<valueField i:nil="true" />
</some_dataField>
</data_containerPerson>
</peopleField>
</data_container>
A.
B
F
这就是它的假设(在visualstuido中生成)
一些数据1
一些数据2
一些数据3
导言
导言
为什么会这样?怎么了?我怎样才能使它正确呢?哦,我只是用了
XmlSerializer
而不是DataContractSerializer
,现在它工作正常了
但是,为什么DataContractSerializer
会这样工作呢
static void xmlGenTest()
{
var doc = new XDocument();
var x = new data_container();
x.other_properties = new data_containerOther_properties { introduction_pic = "a", introduction_txt = "b" };
x.people = new data_containerPerson[1];
x.people[0] = new data_containerPerson
{
p_id = "1",
some_data = new data_containerPersonSome_data { sx = data_containerPersonSome_dataSX.F }
};
using (var writer = doc.CreateWriter())
{
var serializer = new XmlSerializer(x.GetType());
serializer.Serialize(writer, x);
}
Console.WriteLine(doc.ToString());
Console.ReadKey();
doc.Save("C:\\temp\\data.xml");
}
<?xml version="1.0" encoding="utf-8"?>
<data_container xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/">
<other_propertiesField>
<introduction_picField>a</introduction_picField>
<introduction_txtField>b</introduction_txtField>
</other_propertiesField>
<peopleField>
<data_containerPerson>
<p_idField i:nil="true" />
<some_dataField>
<sxField>F</sxField>
<valueField i:nil="true" />
</some_dataField>
</data_containerPerson>
</peopleField>
</data_container>
<?xml version="1.0" encoding="utf-8"?>
<data_container xmlns="http://tempuri.org/XMLSchema.xsd">
<people>
<person p_id="ID1">
<some_data sx="M">some_data1</some_data>
</person>
<person p_id="ID2">
<some_data sx="F">some_data2</some_data>
</person>
<person p_id="ID3">
<some_data sx="M">some_data3</some_data>
</person>
</people>
<other_properties>
<introduction_txt>introduction_txt1</introduction_txt>
<introduction_pic>introduction_pic1</introduction_pic>
</other_properties>
</data_container>
static void xmlGenTest()
{
var doc = new XDocument();
var x = new data_container();
x.other_properties = new data_containerOther_properties { introduction_pic = "a", introduction_txt = "b" };
x.people = new data_containerPerson[1];
x.people[0] = new data_containerPerson
{
p_id = "1",
some_data = new data_containerPersonSome_data { sx = data_containerPersonSome_dataSX.F }
};
using (var writer = doc.CreateWriter())
{
var serializer = new XmlSerializer(x.GetType());
serializer.Serialize(writer, x);
}
Console.WriteLine(doc.ToString());
Console.ReadKey();
doc.Save("C:\\temp\\data.xml");
}