如何要求 XSD.exe 为我生成正确的类,以便我可以创建良好的 XML
我从 Visual Studio 2010 创建了一个 XSD 文件,
然后使用 xsd /c mydemo.xsd 为我生成类,以便我可以在运行时创建 XML 文件。
但是,当我使用该类、填充一些值并序列化该对象时,XML 文件对我来说看起来不太好。
这是我的 XSD 文件单击此处查看
我期望的 XML 文件是什么是点击此处查看 (从 Visual Studio“示例 XML”生成)
但是当我尝试序列化它时,XML 文件是这样的 单击此处查看
格式完全不同
,例如
预期:
<ColumnInfo>
<Column Type="Type1" DisplayValue="DisplayValue1" Key="Key1"/>
<Column Type="Type2" DisplayValue="DisplayValue2" Key="Key2"/>
<Column Type="Type3" DisplayValue="DisplayValue3" Key="Key3"/>
</ColumnInfo>
但生成结果是这样的:
<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>
我实现的序列化报告的代码是:
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 ”,当我使用它时,它会抱怨无法强制转换数组类型......
无法生成临时类(结果=1)。
错误 CS0030:无法将类型“ColumnValueType[]”转换为 '列值类型'
错误CS0029:无法将类型“ColumnValueType”隐式转换为 '列值类型[]'
那么,有人可以给我一些建议吗,我该如何修复我的 XML 格式???
I have created an XSD file from Visual Studio 2010,
Then I use xsd /c mydemo.xsd
to generate class for me, so that I can create a XML file at runtime.
However, when I use that class, fill in some values, and serialize the object, the XML file does not look nice to me.
Here is my XSD file Click here to see
What I expected the XML file to be is Click here to see
(Generated from Visual Studio "Sample XML")
But when I try to serialize it, the XML file is like this CLick here to see
The format is totally different
e.g
Expecting:
<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);
}
Not sure why I cannot use "XmlSerializer", when I use it, it will complain about cannot cast array type something...
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[]'
So, does anyone can give me some suggestion, how can i fix my XML format???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
xsd.exe 上有一个错误 - 请查看此博客文章:
http://satov.blogspot.com/2006/12/xsdexe -生成的类-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
数据协定序列化器具有与 XmlSerializer 不同的规则。为什么不先尝试 XmlSerializer,然后查看输出是否“正确”。
The data contract serializer has different rules that the XmlSerializer. Why don't you try the XmlSerializer first and then see if the output is "correct".