I have multiple Xsd schema files (small ones) and would like to somehow combine them into 1 big file, I was thinking of an xml file with 1 node for each Xsd schema content, in the C# code selecting the corresponding section and validating against it. The problem is I can see a warning "The global element 'xxx' has already been declared. " when opening the Xml file using Visual studio (I'm definitely not an Xsd expert; I indeed have the same element repeated).
Any ideas of the correctness of this approach ? Shall I use CData for Xsd content may be instead ?
Examples of Xml files:
Xml 1:
<resource xmlns="">
<identifier>5401902302111</identifier>
<product>printer</product>
<requestedby />
</resource>
Xml 2:
<resource xmlns="">
<identifier>5401902302112</identifier>
<email>someone@em.com</email>
</resource>
Xsd 1:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="resource">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:long" name="identifier"/>
<xs:element type="xs:string" name="requestedby"/>
<xs:element type="xs:string" name="product"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Xsd 2:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="resource">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:long" name="identifier"/>
<xs:element type="xs:string" name="email"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
What I want to have in the end:
*<?xml version="1.0" encoding="utf-8"?>
<resource>
<resourcedata type="acquisition">
<details>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="resource">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:long" name="identifier"/>
<xs:element type="xs:string" name="requestedby"/>
<xs:element type="xs:string" name="product"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</details>
</resourcedata>
<resourcedata type="warningletter">
<details>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="resource">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:long" name="identifier"/>
<xs:element type="xs:string" name="email"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</details>
</resourcedata>
</data>*
C# code:
var xsdContent =
xDoc.Element("resource").Elements("resourcedata")
.Where(
x =>
x.Attribute("type").Value == "acquisition")
.FirstOrDefault().Element("details").FirstNode.ToString();
var doc = new XDocument(xElementContent);
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdContent)));
Console.WriteLine("Validating doc");
bool errors = false;
doc.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
Console.WriteLine("{0}", e.Exception);
errors = true;
});
Console.WriteLine("doc {0}", errors ? "did not validate" : "validated");
(xElementContent variable contains Xml content)
Thanks