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>
Instead of using
Program
, you should use the class generated from the xsd. When I runIt creates a
schema.cs
file. When I include this in my project, I can write this code:Which writes:
The fact that the
time
property is of typedouble
inschema.cs
means that you can only input a valid number.