2

I have a sample to read a xml schema set for a xml file which contains different namespaces. For this i can get different schema for each namespace as i explained below.

Sample File:

<?xml version="1.0" encoding="utf-8"?>
<data xmlns:d="http://sampleschema/dataservices" xmlns:m="http://sampleschema/dataservices/metadata">
 <content>
  <m:properties>
   <d:CustomerID>ALFKI</d:CustomerID>
   <d:CompanyName>Alfreds Futterkiste</d:CompanyName>
  </m:properties>
 </content>
</data>

Sample Code to get XML Schema Set:

strFileName = @"C:\Sample\Sample.xml";
XmlReader reader = XmlReader.Create(strFileName);
XmlSchemaInference schema = new XmlSchemaInference();
XmlSchemaSet schemaSet = schema.InferSchema(reader);

It gives three different types of schemas while using the above code. But my requirement will be i need a single schema for the entire xml file which contain any number of namespaces in it. I have checked with possibilities of codes in msdn and stack overflow. I can't find any proper solution for this.

The expected schema output will be like below.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
       <xs:element name="data">
              <xs:complexType>
                     <xs:sequence>
                            <xs:element name="content">
                                   <xs:complexType>
                                          <xs:sequence>
                                                 <xs:element name="m:properties">
                                                        <xs:complexType>
                                                               <xs:sequence>
                                                                      <xs:element name="d:CustomerID" type="xs:string"></xs:element>
                                                                      <xs:element name="d:CompanyName" type="xs:string"></xs:element>
                                                                  </xs:sequence>
                                                           </xs:complexType>
                                                    </xs:element>
                                             </xs:sequence>
                                      </xs:complexType>
                               </xs:element>
                        </xs:sequence>
                     <xs:attribute name="xmlns:d" type="xs:string"></xs:attribute>
                     <xs:attribute name="xmlns:m" type="xs:string"></xs:attribute>
                 </xs:complexType>
          </xs:element>
   </xs:schema>

Any one can help to achieve this requirement.

Thanks in advance.

Ajar
  • 162
  • 1
  • 10
  • You can combine schemas together.Put all the namespaces at top of the file. You can have only one xs:schema line so combine the schemas together making sure the "element" tags give correct nesting. The simpleType and complexType are definitions and can be added at the root level. – jdweng Nov 12 '18 at 10:11

1 Answers1

0

Try something like this :

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:m="http://www.w3.org/2001/XMLSchema" xmlns:d="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="data">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="content">
          <m:complexType>
            <m:sequence>
              <m:element name="properties">
                <d:complexType>
                  <d:sequence>
                    <d:element name="CustomerID" type="xs:string"></d:element>
                    <d:element name="CompanyName" type="xs:string"></d:element>
                  </d:sequence>
                </d:complexType>
              </m:element>
            </m:sequence>
          </m:complexType>
        </xs:element>
      </xs:sequence>
      <d:attribute name="xmlns_d" type="d:string"></d:attribute>
      <m:attribute name="xmlns_m" type="d:string"></m:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Hi jdweng, I am asking for c# code to getting the schema as you posted. – Ajar Nov 12 '18 at 10:46
  • There is no c# code to generate schemas. Schemas are hand generated with the aid of tools. Some schemas use multiple URL namespaces and sometimes you have to manually merge the schemas together. For example a SOAP schema only defines the top level header and body. Then there are a lot of unique SOAP the uses the top level standard header and body, but have unique lower level details that are covered in other namespaces. – jdweng Nov 12 '18 at 10:53