【发布时间】:2014-05-22 13:36:22
【问题描述】:
我想根据我的 xsd 架构 (cap.xsd) 生成一个 xml 文件。我找到了这篇文章并按照说明进行操作: Generating XML file using XSD file
- 我在 xsd.exe 的帮助下创建了该类,并通过拖放将其插入到我的解决方案中
- 之后,我构建了我的解决方案并创建了 xml。但它不基于 xsd 架构。
xml 文件有一个包含字符的元素,但架构说必须有数字(双)
-
无论如何,我看不出 xsd 架构对生成的 xml 有什么影响?如果我删除架构,xml 文件仍然被创建。并且 xml 文件是在这一行创建的:
var data = 新程序 { 时间 = "abc", 来源=“443543253243”, };
.. 而不是我的架构:
怎么了?
我的班级:
namespace testapp
{
using System.IO;
using System.Xml.Serialization;
public class Program
{
public string Time;
public string Source;
public static void Main()
{
var data = new Program
{
Time = "abc",
Source = "buffalo",
};
var serializer = new XmlSerializer(typeof(Program));
using (var stream = new StreamWriter("E:\\cap_test.xml"))
{
serializer.Serialize(stream, data);
}
}
}
}
我的架构:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="cap" type="capType"/>
<xsd:complexType name="capType">
<xsd:sequence>
<xsd:element name="tel" type="telType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="telType">
<xsd:sequence>
<xsd:element name="time" type="xsd:double"/>
<xsd:element name="source" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
还有我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<Program xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Time>abc</Time>
<Source>buffalo</Source>
</Program>
【问题讨论】:
标签: c# .net xml xsd xml-serialization