Saturday, April 23, 2011

Creat an XML file based on an XSD schema

I have an XSD file pre defined as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="Synchro">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="Titles" minOccurs="0">
     <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
       <xs:element name="Name" type="xs:string"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
 </xs:complexType>
</xs:schema>
Using a dataset I populate it as follows:
// Create an instance of a dataset
DataSet syncDS = new DataSet();

// Assign the schema to the dataset
syncDS.ReadXmlSchema(@"c:\temp\Synchro.xsd");

// Populate the tittles table
syncDS.Tables["Titles"].Rows.Add("Ms");
syncDS.Tables["Titles"].Rows.Add("Dr");
syncDS.Tables["Titles"].Rows.Add("Professor");
syncDS.Tables["Titles"].Rows.Add("Mr");
syncDS.Tables["Titles"].Rows.Add("Miss");
syncDS.Tables["Titles"].Rows.Add("Sir");
syncDS.Tables["Titles"].Rows.Add("Mrs");
syncDS.Tables["Titles"].Rows.Add("Reverend");

syncDS.WriteXml(@"c:\temp\syncro.xml");
ANSWER
--------------
I have worked out how to solve the problem but I'm not convinced it's 
the most efficient way of doing things but at least I know it works.
 
 Below, I've included some sample code:
// create the root element
XElement root = new XElement("Synchro", null);

// Add the attributes to the root node
root.Add(new XAttribute("SchemaVersion", "1.0.1")); // this is hard coded as it must be changed when the XSD structure changes
root.Add(new XAttribute("SynchroDate", DateTime.Today.ToString("yyyy-MM-dd")));

// Now populate the titles element

// Create the Titles element
XElement Titles = new XElement("Titles", null);

//Hard code for test case
//Titles.Add(new XElement("Name", "Ms"));
//Titles.Add(new XElement("Name", "Dr"));
//Titles.Add(new XElement("Name", "Professor"));
//Titles.Add(new XElement("Name", "Mr"));
//Titles.Add(new XElement("Name", "Miss"));
//Titles.Add(new XElement("Name", "Sir"));
//Titles.Add(new XElement("Name", "Mrs"));
//Titles.Add(new XElement("Name", "Reverend"));

// Iterate through the values from a datatable 
foreach (SyncDataSet.TitlesRow tRow in SyncData.SyncTitles)
{
    // Create a new element node for each title
    Titles.Add(new XElement("Name", tRow.TitleName));
}

// Add the Titles element to the root node
root.Add(Titles);

// Validate the root node has been populated correctly against the schema
XDocument doc = new XDocument(root);

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(XmlSchema.Read(new StreamReader(Path.Combine(SettingsEx.Instance.SchemasFolder, "BAHCC_Synchro.xsd")), null));

// set the valid flag to true prior to validation and let the event handler set it to false if it's not valid
bool OutputValid = true;

// Validate against the schema and set the Output valid flag to false if the xmlSync_validation event handler is called.
doc.Validate(schemas, xmlSync_validation);

if (OutputValid == true)
{
    // Output the XML to a file
    root.Save(@"C:\temp\Synchro.xml"));
}
This produces an output of :
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Titles>
    <Name>Ms</Name>
  </Titles>
  <Titles>
    <Name>Dr</Name>
  </Titles>
  <Titles>
    <Name>Professor</Name>
  </Titles>
  <Titles>
    <Name>Mr</Name>
  </Titles>
  <Titles>
    <Name>Miss</Name>
  </Titles>
  <Titles>
    <Name>Sir</Name>
  </Titles>
  <Titles>
    <Name>Mrs</Name>
  </Titles>
  <Titles>
    <Name>Reverend</Name>
  </Titles>
</NewDataSet>
but what I was expecting something like this:
<Synchro SchemaVersion="1.0.1" SynchroDate="2009-12-11">
    <Titles>
      <Name>Ms</Name>
      <Name>Dr</Name>
      <Name>Professor</Name>
      <Name>Mr</Name>
      <Name>Miss</Name>
      <Name>Sir</Name>
      <Name>Mrs</Name>
      <Name>Reverend</Name>
    </Titles>
  </Synchro> for ref:-
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/4df9785a-8b1f-48c7-9914-1ead9da3ce82/ 

No comments:

Post a Comment