Reputation: 1792
(Related to my question XML Parsing using a schema in C#)
I'm working on some code for an app that will use XML for storing business objects (they're designed to be quite small, so I figured that XML might be easier to parse, deal with and check rather than using text files or some custom file format). I've already written the schema, and was wondering how I would go about adding a reference to this schema in the outputted XML files.
As with the question I've linked, I'm using XmlDocument to write the files, because I'm having to work in .net 3.5 - same reasons as the question I linked to. I'm also using XmlDeclaration, XmlNode and XmlElement to write out the individual pieces of the xml file to the XmlDocument object.
Here's an example of what I'm doing:
if (!File.Exists(fileName))
{
XmlDocument xmlDocumentWriter = new XmlDocument();
XmlDeclaration xmlDec = xmlDocumentWriter.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDocumentWriter.AppendChild(xmlDec);
XmlNode root = xmlDocumentWriter.CreateElement("root");
XmlElement childNodeInfo = xmlDocumentWriter.CreateElement("parent-of-some-child");
root.AppendChild(childNodeInfo);
childNodeInfor.AppendChild(xmlDocumentWriter.CreateElement("SomeChild")).InnerText = _somevariable.ToString();
/* continue in this way until they've all been added */
xmlDocumentWriter.Appendchild(root);
xmlDocumentWriter.Save(fileName);
/* close all open streams and xml objects */
}
Which will create an xml file like this:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<parent-of-some-child>
<somechild>value</somechild>
</parent-of-some-child>
</root>
But, how would I go about adding a schema reference to the root node?
The schema I've written looks similar to this:
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:schema-name"
elementFormDefault="qualified">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="parent-of-some-child">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="some-child" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element name="parent-of-some-child">
</xsd:sequence>
</xsd:complexType>
</xsd:element name="root">
I'm looking to create something like this:
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="reference-to-schema">
<parent-of-some-child>
<somechild>value</somechild>
</parent-of-some-child>
</root>
Or do I even need a reference to the schema in the xml file? Would using the schema to check the xml during opening and parsing be enough?
Upvotes: 1
Views: 1236
Reputation: 2466
Here is an example :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace XmlNameSpace
{
class Program
{
static void Main(string[] args)
{
XNamespace xmlns = XNamespace.Get("urn:schema-name");
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace schemaLocation = XNamespace.Get("urn:schema-name schema.xsd");
XDocument p = new XDocument(
new XElement(xmlns + "root",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", schemaLocation),
new XElement("parent-of-some-child",
new XElement("somechild","value"))
)
);
p.Save("c:\\test.xml");
}
}
}
Upvotes: 1