Load XML matched Special Schema by Deserialize

1. How to generate schema file from existed XML file

answer: run command-line under vs2010 tools

            switch current location to location that XML is putted in

            run: xsd XML

            then, a schema with same name with XML is generated.

We also can adjust the schema in XML Editor

2. After change data in previous XML file, 

    How to re-get the data from it.

         Deserialize       

3. A method name: LoadXMLonSpecialSchema(Object myobject, string XML)

The flow of it is:

    XmlSerializer serailizer = new XMLSerializer( System.Type of Object);

this object is an instance of root class that will be used for store the data from XML contain entire root.

But, how to create his class, 

we also can as XSD.exe for help:

xsd name.xsd /classes


      Stream streader = new FileStream(XML, FileMode.open);

      XMLReader reader = XmlReader.Create(streader);

      myobject = serializer.Derialize(reader);

      reader.close();

      return myobject

It is over.


During finish this i meet some problem:

1. Derailize report error when i use "TestReader"/"StreamReader" instead of Stream/FileStream and XMLReader

2. Derialize report error as below:

unable to generate a temporary class(result=1) and error CS0030 and CS0029.

This is a bug that Microsoft will not fix it.

It happen to this scenario that is:

 The schema contain a complex element and in this elment only contain a complex element and whose "maxOccurs" value is "unbounded".

How to avoid it:

Have to add a property <xs:attribute name="tmp" type="xs:string"> to the parent element.

After this, we can get a sample like this:

<targets p2:tmp="tmp1" xmln:p2="http://********">

<p2:target>

<p2:source>sourcefile1</p2source>

<p2:destination>destination2</p2:destination>

</p2target>

<p2:target>

<p2:source>sourcefile1</p2source>

<p2:destination>destination2</p2:destination>

</p2target>

</targets>


All this finished, the deserialized can successfully.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值