2

I try to dynamically create an XML schema (XSD) from C#, using the conventional XElement and XAttribute classes, but it is not valid to specify any names with colons. That is, I cannot create the element <xs:element> using the code

... = new XElement("xs:element");

because ":" is not allowed.

What is the correct way of dynamically building a schema in C# then?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
someName
  • 1,275
  • 2
  • 14
  • 33

4 Answers4

2

To create schemas, you should be using the XmlSchema class. The link below provides a comprehensive example of creating one programmatically:

http://msdn.microsoft.com/en-us/library/9ta3w88s.aspx


Example:

static void Main(string[] args)
{
    var schema = new XmlSchema();

    // <xs:element name="myElement" type="xs:string"/>
    var myElement = new XmlSchemaElement();
    schema.Items.Add(myElement);
    elementCat.Name = "myElement";
    elementCat.SchemaTypeName = 
        new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

    // writing it out to any stream
    var nsmgr = new XmlNamespaceManager(new NameTable());
    nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
    schema.Write(Console.Out, nsmgr);

    Console.ReadLine();
}
code4life
  • 15,655
  • 7
  • 50
  • 82
1

When creating new XML elements, you should be aware that the part before the colon (in this case, xs) is actually an alias for the XML namespace (in the case of an XSD, xs usually refers to http://www.w3.org/2001/XMLSchema). So, to continue using XDocument to build your XSD, you would want to use:

XNamespace ns = new XNamespace("http://www.w3.org/2001/XMLSchema");
... = new XElement(ns + "element");

See the example here: http://msdn.microsoft.com/en-us/library/bb292758.aspx

Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69
  • But that would give me an element that looks like instead of ... – someName Mar 29 '11 at 14:34
  • It does mean, semantically, the same thing. If you want the abbreviation, I believe you can add the definitions as Marc specified here: http://stackoverflow.com/questions/1265378/how-do-i-add-multiple-namespace-declarations-to-an-xdocument, though I haven't tried it myself. – Matt DeKrey Mar 29 '11 at 15:59
  • Thanks! together with http://stackoverflow.com/questions/1338517/how-can-i-write-xml-with-a-namespace-and-prefix-with-xelement this answered my question! :-) – someName Mar 30 '11 at 04:25
0

I wrote a blog about that very subject. You can use a DataTable to save a schema.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • I would really like to create it explicitly (instead of through intermediate data structures) to gain full control. I would be able to do this using XElement and XAttribute if just they allowed ":" in the names. – someName Mar 29 '11 at 14:22
  • -1: @Steve, aren't there a few restrictions if you use a `DataTable`? Like, you could only create XML schema that maps to a relational model? – John Saunders Mar 30 '11 at 01:09
  • @John, Sure there are restrictions. It creates schemas that describe XML structures that can be read in by DataTables. Depending on your needs, that's either a benefit or a drawback. – Steve Wellens Mar 30 '11 at 21:36
  • @John...the big benefit is that you don't have to learn the schema language. – Steve Wellens Mar 30 '11 at 21:47
  • if the OP had said he was interested in restricting to "relational" data, then I wouldn't have downvoted. The absence of any constraints made my downvote an answer which places constraints on the schema which can be produced. – John Saunders Mar 31 '11 at 20:39
0

If you want to create xml, you should use XmlWriter class

archil
  • 39,013
  • 7
  • 65
  • 82
  • I mean that xsd schema is an xml. XmlWriter http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx has all the methods to create custom xml – archil Mar 29 '11 at 14:30
  • did you know that `XmlDocument` or LINQ to XML can also be used to create "custom XML", and a lot more easily that using `XmlWriter` directly? – John Saunders Mar 30 '11 at 01:09
  • @John Saunders: No i didnt. I think `XmlDocument` or is better when manipulating with existing xml, as they support navigating and editing too. As from MSDN - "`XmlWriter` Represents a writer that provides a fast, non-cached, forward-only means of generating streams or files containing XML data.". You're right - LINQ to Xml is simpler to use, but isn't in built on top of `XmlWriter`? – archil Mar 30 '11 at 06:12
  • yes, it's built on XmlWriter, which makes it much easier to use than XmlWriter. And I didn't say "better than", I said "also". Both XmlDocument and LINQ to XML are much easier to use for this purpose that a raw XmlWriter. – John Saunders Mar 31 '11 at 20:35