如何让 XSD.exe 为我生成合适的类,以便我可以创建一个很好的 XML [英] how to ask XSD.exe to generate proper class for me, so that i can create a fine XML

查看:27
本文介绍了如何让 XSD.exe 为我生成合适的类,以便我可以创建一个很好的 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Visual Studio 2010 创建了一个 XSD 文件,

I have created an XSD file from Visual Studio 2010,

然后我使用 xsd/c mydemo.xsd 为我生成类,这样我就可以在运行时创建一个 XML 文件.

Then I use xsd /c mydemo.xsd to generate class for me, so that I can create a XML file at runtime.

然而,当我使用那个类,填充一些值并序列化对象时,XML 文件对我来说并不好看.

However, when I use that class, fill in some values, and serialize the object, the XML file does not look nice to me.

这是我的 XSD 文件 点击此处查看

Here is my XSD file Click here to see

我期望的 XML 文件是单击此处查看(从 Visual Studio示例 XML"生成)

What I expected the XML file to be is Click here to see (Generated from Visual Studio "Sample XML")

但是当我尝试序列化它时,XML文件是这样的点此查看

But when I try to serialize it, the XML file is like this CLick here to see

格式完全不同

例如

期待:

<ColumnInfo> 
  <Column Type="Type1" DisplayValue="DisplayValue1" Key="Key1"/> 
  <Column Type="Type2" DisplayValue="DisplayValue2" Key="Key2"/> 
  <Column Type="Type3" DisplayValue="DisplayValue3" Key="Key3"/> 
</ColumnInfo>

但是生成的结果是这样的:

but the generate result is like this:

<columnInfoField>
  <ColumnType>
    <displayValueField>Display value for key 1</displayValueField>
    <keyField>key1</keyField>
    <typeField>string</typeField>
  </ColumnType>
  <ColumnType>
    <displayValueField>Display value for key 2</displayValueField>
    <keyField>key2</keyField>
    <typeField>int</typeField>
  </ColumnType>
  <ColumnType>
    <displayValueField>Display value for key 3</displayValueField>
    <keyField>key3</keyField>
    <typeField>long</typeField>
  </ColumnType>
</columnInfoField>

我实现的用于序列化报告的代码是:

And the code I implement to serialize the report is :

http://msdn.microsoft.com/en-us/library/ms731073.aspx

DataContractSerializer dcs = new DataContractSerializer(typeof(Report));
using (XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateTextWriter(File.Create(@"C:\demo\schema\output.xml"), Encoding.UTF8))
{
  dcs.WriteObject(xdw, report);
}

不知道为什么我不能使用XmlSerializer",当我使用它时,它会抱怨无法转换数组类型的东西...

Not sure why I cannot use "XmlSerializer", when I use it, it will complain about cannot cast array type something...

无法生成临时类(结果=1).
错误 CS0030:无法将类型ColumnValueType[]"转换为'列值类型'
错误 CS0029:无法将类型ColumnValueType"隐式转换为'列值类型[]'

Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'ColumnValueType[]' to 'ColumnValueType'
error CS0029: Cannot implicitly convert type 'ColumnValueType' to 'ColumnValueType[]'

那么,有没有人可以给我一些建议,我该如何修复我的 XML 格式???

So, does anyone can give me some suggestion, how can i fix my XML format???

推荐答案

xsd.exe 有一个错误 - 请看这篇博文:http://satov.blogspot.com/2006/12/xsdexe-generated-classes-causing.html

There is a bug on xsd.exe - look at this blog-post: http://satov.blogspot.com/2006/12/xsdexe-generated-classes-causing.html

这篇关于如何让 XSD.exe 为我生成合适的类,以便我可以创建一个很好的 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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