序列化后xml内容改变&反序列化想传入xml来映射对象 [英] xml content changed after serialization & deserialization want to pass in xml to map object

查看:65
本文介绍了序列化后xml内容改变&反序列化想传入xml来映射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙:

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

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 定义:

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 看起来像这样的后记:

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.

推荐答案

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

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

这篇关于序列化后xml内容改变&amp;反序列化想传入xml来映射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆