Reputation: 11649
I believed that we create XML's Schema
s to define the XML elements and its structure. I thought, we must define the XSD
before we create a XML
file. In other word, xml
file without its xsd
is meaningless.
Today I run this code,
XmlWriter XmlWriter = XmlWriter.Create("Test.xml");
XmlWriter.WriteStartDocument();
XmlWriter.WriteStartElement("Users");
XmlWriter.WriteStartElement("User");
XmlWriter.WriteStartAttribute("Salman", "24");
XmlWriter.WriteString("Salman");
XmlWriter.WriteEndElement();
XmlWriter.WriteEndDocument();
XmlWriter.Close();
When I find my output, there was not any XSD
file. Only Test.xml
.
So my question is:
What is the exact role of XSD
files?
If I were right, why doesn't the C# generate the output?
Upvotes: 0
Views: 413
Reputation: 4146
XSDs are used for validation of XML files, but are optional and not required. If you are writing / reading from the same application, and you control all access to the XML file with error/exception handling for when things don't work, you don't need the XSD.
When passing data between applications, the XSD serves as the contract that ensures both parties understand and agree on the format of the XML file being passed.
Upvotes: 3