xml content changed after serialization & deserial

2019-09-11 15:41发布

Please help:

What I need to do: I pass in an XML file from a browser bar, then try to map the data in the XML files to a object type in a class.

There are many different objects types, so if I write classes for each serialization & deserialization, that would be too much work.

I searched some code for serialization & deserialization and tried them, they compile, but many of the data are missing.

My code for serialization & deserialization are here:

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

The Original Xml is:

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

The xml looks like this afterwords:

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

Could Any please tell me what's wrong or tell me what I should do to finish my goal? Great thanks.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-11 16:16
byte[] byteArray = Encoding.UTF8.GetBytes(xmlContent);
MemoryStream tempMemoryStream = new MemoryStream(byteArray);
DataContractSerializer serializer = new DataContractSerializer(typeof(ClassType));
ClassType variableName= (ClassType)serializer.ReadObject(TempMemoryStream);

This works for a type, using the data contract serializer, convert the xml into a stream then deserialize it. (in my case, the input xml file was pre-processed to match the format already. thanks to lee, and all the people who helped. Cheers! :D

查看更多
登录 后发表回答