XML序列化的工作方式与预期不符

时间:2014-04-01 04:03:25

标签: c# xml xsd

我得到了一个具有这种结构的.xsd文件(我也使用其中一个Visual Studio工具为它生成了类)

  <!-- <?xml version="1.0" encoding="utf-8"?>
        <xs:schema...  scheme and etc here--> 
          <xs:element name="data_container" >
            <xs:complexType>
              <xs:sequence>

                <xs:element name="people">
                  <xs:complexType>
                    <xs:sequence>

                      <xs:element name="person"  maxOccurs="unbounded">

                        <xs:complexType>
                          <xs:sequence>

                            <xs:element name="some_data">
                              <xs:complexType>
                                <xs:simpleContent>
                                  <xs:extension base="xs:string">
                                    <xs:attribute name="sx" use="required" >
                                      <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                          <xs:enumeration value="M"/>
                                          <xs:enumeration value="F"/>
                                        </xs:restriction>
                                      </xs:simpleType>
                                    </xs:attribute>
                                  </xs:extension>
                                </xs:simpleContent>
                              </xs:complexType>
                            </xs:element>

                          </xs:sequence>
                          <xs:attribute name="p_id" type="xs:ID" use="required" />
                        </xs:complexType>
                      </xs:element>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>

                <xs:element name="other_properties">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="introduction_txt"  type="xs:string"/>
                      <xs:element name="introduction_pic"  type="xs:string"/>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>

              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:schema>

所以我像这样生成我的xml

 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 DataContractSerializer(x.GetType());
                serializer.WriteObject(writer, x);
            }

            Console.WriteLine(doc.ToString());
            Console.ReadKey();
            doc.Save("C:\\temp\\data.xml");

        }

这就是我的方法生成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>

这就是假设(在Visual Stuido中生成)

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

为什么会这样?怎么了?我怎么能做对的?

1 个答案:

答案 0 :(得分:1)

哦,我刚用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");

        }