序列化后xml内容发生了变化。反序列化想要将xml传递给map对象

时间:2013-09-19 15:52:38

标签: c# xml object xml-serialization xml-deserialization

请帮助:

我需要做什么:从浏览器栏传入XML文件,然后尝试将XML文件中的数据映射到类中的对象类型。

有许多不同的对象类型,所以如果我为每个序列化编写类和反序列化,这将是太多的工作。

我搜索了一些代码序列化的代码。反序列化和尝试它们,他们编译,但许多数据丢失。

我的序列化代码&反序列化在这里:

string InputFilePath = FileUpload1.PostedFile.FileName;

MyServiceTypeClass _MyServiceTestObj = new MyServiceTypeClass ();
XmlSerializer SerializerObj = new XmlSerializer(typeof(MyServiceTypeClass ));
StreamWriter WriteFileStream = new StreamWriter(@InputFilePath);
SerializerObj.Serialize(WriteFileStream, _MyServiceTestObj );
WriteFileStream.Close();

FileStream XmlStream = new FileStream(@InputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
MyServiceTypeClass _ServiceTypeLoaded = (MyServiceTypeClass )SerializerObj.Deserialize(XmlStream);
XmlStream.Close();

MyServiceTypeClass Defn:

public class MyServiceType: UpperLevel1
    {
        [DataMember]
        public TypeDefinition Definition;
        [DataMember]
        public Type Type;
        [DataMember]
        public string a;
        [DataMember]
        public double? b;
    }

public class UpperLevel1: UpperLevel2
    {
        [DataMember(IsRequired = true)]
        public double a;
        [DataMember]
        public double? b;
        [DataMember]
        public double? c;
        [DataMember]
        public double? d;
        }

etc until 5 levels

原始Xml是:

<?xml version="1.0" encoding="utf-8" ?>
<ns0:BondTransaction xmlns:ns0="ABC" xmlns:ns1="http://schemas.microsoft.com/2003/10/Serialization/">
  <ns0:a>0</ns0:a>
  <ns0:m1>Something</ns0:m1>
  <ns0:b>Remain1</ns0:b>
  <ns0:m2> Something </ns0:m2>
   <ns0:m2a>
     <ns0:m2b>
       <ns0:m2c>
         <ns0:m2d>
          <ns0:m2d1>Position</ns0:m2d1>
          <ns0:m2d2>BAR</ns0:m2d2>
        </ns0:m2d>
      </ns0:m2c>
      <ns0:m2b2>Remain2</ns0:m2b2>
    </ns0:m2b>
  </ns0:m2a>
  <ns0:m3>12345</ns0:m3>
  <ns0:m4>SomeCOMPANY</ns0:m4>
  <ns0:m5>TRAD</ns0:m5>
  <ns0:m6>12345</ns0:m6>
  <ns0:m7>Time</ns0:m7>
  <ns0:SomeDesign>
     <ns0:Property>
      <ns0:Type>type1</ns0:Type>
      <ns0:Value>somevalue</ns0:Value>
    </ns0:Property>
  </ns0:SomeDesign>
  <ns0:Price>1</ns0:Price>
  <ns0:QuotationCurrency>USD</ns0:QuotationCurrency>
   <ns0:SomeOtherDesign>
     <ns0:Property>
      <ns0:Type>typ2</ns0:Type>
      <ns0:Value>gfgg</ns0:Value>
    </ns0:Property>
     <ns0:Property>
      <ns0:Type>fer</ns0:Type>
      <ns0:Value>getrt</ns0:Value>
    </ns0:Property>
  </ns0:SomeOtherDesign>
  <ns0:Exchange>hmmm</ns0:Exchange>
</ns0:BondTransaction>

xml看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
- <big xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <a>0</a>
  <b>INSERT</b>
  <g xsi:nil="true" />
  <r xsi:nil="true" />
  <rw>AGAINST</rw>
  <rwer xsi:nil="true" />
  <gfg>TRAD</gfg>
  <a xsi:nil="true" />
  <b xsi:nil="true" />
  <c xsi:nil="true" />
  <d xsi:nil="true" />
  <e xsi:nil="true" />
  <f xsi:nil="true" />
  <h xsi:nil="true" />
  <sd xsi:nil="true" />
  <s xsi:nil="true" />
  <gf>0</gf>
  <y xsi:nil="true" />
  <ted xsi:nil="true" />
  <ty xsi:nil="true" />
  <yt xsi:nil="true" />
  <uy>DEFAULT</uy>
  <tert xsi:nil="true" />
  <tr xsi:nil="true" />
</big>

有什么可以告诉我有什么问题或者告诉我应该怎样做才能完成我的目标?十分感谢。

1 个答案:

答案 0 :(得分:0)

byte[] byteArray = Encoding.UTF8.GetBytes(xmlContent);
MemoryStream tempMemoryStream = new MemoryStream(byteArray);
DataContractSerializer serializer = new DataContractSerializer(typeof(ClassType));
ClassType variableName= (ClassType)serializer.ReadObject(TempMemoryStream);

这适用于类型,使用数据协定序列化程序,将xml转换为流然后反序列化它。 (在我的情况下,输入的xml文件已经过预处理以匹配格式。感谢lee和所有帮助过的人。干杯!:D