Question

For the below XML, I need to generate XSD created but getting an error

The 'NewDataSet' element is not declared

<NewDataSet>
 <Table>
  <SITE>VMD</SITE>
  <TANK>65-12-392</TANK>
  <SERVICE>HZLPG</SERVICE>
  <IP21TAG>BC-BBH-OS-4LI21392</IP21TAG>
 </Table>
</NewDataSet>

XSD:

<?xml version="1.0"?>
 <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PAS">
 <xs:complexType>
   <xs:sequence>
<xs:element name="Records">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Site" type="xs:string" />
      <xs:element name="Plant" type="xs:string" />
      <xs:element name="Tank" type="xs:string" />
      <xs:element name="Service" type="xs:string" />
      <xs:element name="IP21Tag" type="xs:string" />
    </xs:sequence>
   </xs:complexType>
  </xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Can anyone please help how to declare 'NewDataSet' element? Thanks in advance.

Was it helpful?

Solution 2

to start with, the root element 'NewData' is not declared in your XSD. I would suggest you take a look at this first: http://www.w3schools.com/schema/schema_example.asp

OTHER TIPS

You can generate an XSD from example XML using the xsd.exe supplied from Microsoft. You will only generate an XSD which matches your example so you would need an example to contain all the cases that you wished to parse, or you would have to further edit the XSD to include those.

Anyway this is a good way to get started. MSDN Docs on XSD.exe

Once you have a XSD file you might want to use a tool like XSD2Code which will generate all the code you need to read the XML and turn it into a set of c# objects in memory.

(This assumes you can read all your XML into memory in one go. Otherwise you will need to read your XML using an event SAX type approach.)

XmlSchemaInference Class can be used for coverting xml to xsd like:

        XmlReader reader = XmlReader.Create ( "contosoBooks.xml" );  
        XmlSchemaSet schemaSet = new XmlSchemaSet ( );  
        XmlSchemaInference schema = new XmlSchemaInference ( );  
        schemaschemaSet = schema.InferSchema ( reader );  
        foreach ( XmlSchema s in schemaSet.Schemas ( ) )  
        {  
            s.Write ( Console.Out );  
        } 

http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemainference.aspx

Second way,

Here is example:

Process p = new Process(); 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "Xsd.exe"; 
p.StartInfo.Arguments = "C:\\config.xml /outputdir:C:\\Temp"; 
p.Start(); 
string output = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 
Console.WriteLine("OUTPUT FROM XSD.EXE : " + output); 

This will create config.xsd file from config.xml.

All credit goes to people who answered it on msdn forums.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top