C# 基于xsd模式生成xml(使用.NET)

C# 基于xsd模式生成xml(使用.NET),c#,.net,xml,xsd,xml-serialization,C#,.net,Xml,Xsd,Xml Serialization,我想根据我的xsd模式(cap.xsd)生成一个xml文件。我找到了这篇文章并按照说明进行了操作: 我在xsd.exe的帮助下创建了这个类,并通过拖放将其插入到我的解决方案中 之后,我构建了我的解决方案,并创建了xml。但它并不基于xsd模式 xml文件中有一个包含字符的元素,但模式表示该元素必须有数字(双精度) 无论如何,我看不出xsd模式对生成的xml有什么影响?如果删除模式,xml文件仍然会被创建。xml文件是在这一行创建的: var数据=新程序 { Time=“abc”, Sourc

我想根据我的xsd模式(cap.xsd)生成一个xml文件。我找到了这篇文章并按照说明进行了操作:

  • 我在xsd.exe的帮助下创建了这个类,并通过拖放将其插入到我的解决方案中
  • 之后,我构建了我的解决方案,并创建了xml。但它并不基于xsd模式
  • xml文件中有一个包含字符的元素,但模式表示该元素必须有数字(双精度)

  • 无论如何,我看不出xsd模式对生成的xml有什么影响?如果删除模式,xml文件仍然会被创建。xml文件是在这一行创建的:

    var数据=新程序 { Time=“abc”, Source=“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 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>
我的模式:

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 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>

和我的xml文件:

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 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>

abc
水牛

您应该使用从xsd生成的类,而不是使用
程序。当我跑的时候

xsd /classes schema.xsd
它创建一个
schema.cs
文件。当我将其包含在项目中时,我可以编写以下代码:

class Program
{
    public static void Main()
    {
        var data = new capType { tel = new[] {
           new telType { source = "buffalo", time = 1 }
        } };

        var serializer = new XmlSerializer(typeof(capType));
        using (var stream = new StreamWriter(@"E:\cap_test.xml"))
        {
            serializer.Serialize(stream, data);
        }
    }
}
其中写道:

<?xml version="1.0" encoding="utf-8"?>
<cap xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <tel>
    <time>1</time>
    <source>buffalo</source>
  </tel>
</cap>

1.
水牛
time
属性在
schema.cs
中的类型为
double
,这意味着您只能输入有效的数字