generating xml based on xsd schema (with .NET)

2019-04-01 00:26发布

I want to generate a xml file, based on my xsd schema (cap.xsd). I found this article and followed the instruction: Generating XML file using XSD file

  • I've created the class with help of xsd.exe and inserted it by drag and drop into my solution
  • After that, I build my solution and the xml was created. But it doesn't based on the xsd schema.
  • The xml file had a element which contained characters, but the schema says that must have numbers (double)

  • Anyway, I don't see which effect the xsd schema has to the generated xml? If I delete the schema, the xml file still was created. And the xml file was created this line:

    var data = new Program { Time = "abc", Source = "443543253243", };

.. and not by my schema:

What's wrong?


My class:

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);
            }
        }
    }
}

My schema:

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

and my xml file:

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

1条回答
叛逆
2楼-- · 2019-04-01 01:14

Instead of using Program, you should use the class generated from the xsd. When I run

xsd /classes schema.xsd

It creates a schema.cs file. When I include this in my project, I can write this code:

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);
        }
    }
}

Which writes:

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

The fact that the time property is of type double in schema.cs means that you can only input a valid number.

查看更多
登录 后发表回答