Writing an XMl file using a specified schema

G

Guest

Help! I created a XML schema with a Visual Studio tools. I'm filling a
dataset with a DataAdapter. Before I use the "WriteXml" method to write the
data to a xml file, I want to map the XSD file I created to the dataset so
that when I do use the "WriteXml" method, the generated xml file will be
properly formatted to the schema I created. I did try using the
"ReadXMLSchema" but it didn't work. Please help me!
 
R

rhaazy

You may be approaching this the wrong way, I recently had a problem
going from a dataset to xml and found the columnmapping property of the
dataset in combination with relation objects to work very well

for example:

ds.Relations.Add(new DataRelation("Appsettings AConfiguration",
ds.Tables["Appsettings"].Columns[0],
ds.Tables["AConfiguration"].Columns[0]));
ds.Relations.Add(new DataRelation("Profile Configuration",
ds.Tables["Profile"].Columns[0],
ds.Tables["Configuration"].Columns[1]));

ds.Relations[0].Nested = true;
ds.Relations[1].Nested = true;

ds.Tables["Appsettings"].Columns["ID"].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[0].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[1].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[2].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[3].ColumnMapping =
MappingType.SimpleContent ;
ds.Tables["Profile"].Columns["ID"].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[0].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[1].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[2].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[3].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[4].ColumnMapping =
MappingType.SimpleContent ;

the relation objects create the parent child relationship between the
columns, and the nesting property sets it up for use in your XML
document.
the columnmapping stuff is pretty self explanatory.

Hope this helps.
 
G

Guest

I don't really understand what you are trying to expain....
Here is an example of what i am doing:

This is the xsd:
<xs:element name="Flight" type="Flight"/>
<xs:complexType name="Flight">
<xs:sequence>
<xs:element name="DepartureDate" type="xs:date" />
<xs:element name="Origin" type="xs:string" />
<xs:element name="Destination" type="xs:string" />
<xs:element name="OutOfStock" type="OutOfStock" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="FlightNo" type="xs:string" />
<xs:attribute name="LegIndex" type="xs:int" />
</xs:complexType>

<xs:complexType name="OutOfStock">
<xs:sequence>
<xs:element name="Product" type="xs:int" />
<xs:element name="Row" type="xs:string" />
</xs:sequence>
</xs:complexType>

Here is when i fill the dataset:
DataSet ds = new DataSet();
ds.ReadXmlSchema(@"E:\Schema.xsd");
SqlConnection conn = getConnection;
SqlDataAdapter da = new SqlDataAdapter("select * from
vFlights", conn);
da.Fill(ds, "Flight");
ds.WriteXml("flights.xml");

My problem is that the "OutOfStock" element doesn't show, it just shows the
"Product" and "Row" but there are not nested inside the "OutOfStock" tag. I
look at the schema generated after with a ds.WriteXMLSchema(".."); and it
seemed to take into consideration the specified schema but it also overloaded
with its owe...

It generated this:
<Flight FlightNo="222" LegIndex="1">
<DepartureDate>2006-07-25</DepartureDate>
<Origin>RRR</Origin>
<Destination>GGG</Destination>
<Row>23</Row>
<Product>1</Product>
</Flight>

But i want it to generate this:
<Flight FlightNo="222" LegIndex="1">
<DepartureDate>2006-07-25</DepartureDate>
<Origin>RRR</Origin>
<Destination>GGG</Destination>
<OutOfStock>
<Row>23</Row>
<Product>1</Product>
</OutOfStock>
</Flight>

thanks, maybe this will clarify things!
rhaazy said:
You may be approaching this the wrong way, I recently had a problem
going from a dataset to xml and found the columnmapping property of the
dataset in combination with relation objects to work very well

for example:

ds.Relations.Add(new DataRelation("Appsettings AConfiguration",
ds.Tables["Appsettings"].Columns[0],
ds.Tables["AConfiguration"].Columns[0]));
ds.Relations.Add(new DataRelation("Profile Configuration",
ds.Tables["Profile"].Columns[0],
ds.Tables["Configuration"].Columns[1]));

ds.Relations[0].Nested = true;
ds.Relations[1].Nested = true;

ds.Tables["Appsettings"].Columns["ID"].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[0].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[1].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[2].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[3].ColumnMapping =
MappingType.SimpleContent ;
ds.Tables["Profile"].Columns["ID"].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[0].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[1].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[2].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[3].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[4].ColumnMapping =
MappingType.SimpleContent ;

the relation objects create the parent child relationship between the
columns, and the nesting property sets it up for use in your XML
document.
the columnmapping stuff is pretty self explanatory.

Hope this helps.
Help! I created a XML schema with a Visual Studio tools. I'm filling a
dataset with a DataAdapter. Before I use the "WriteXml" method to write the
data to a xml file, I want to map the XSD file I created to the dataset so
that when I do use the "WriteXml" method, the generated xml file will be
properly formatted to the schema I created. I did try using the
"ReadXMLSchema" but it didn't work. Please help me!
 
R

rhaazy

What I am explaining is that you don't need a schema to generate an XML
document from a dataset.

You can use the column mapping property of the dataset like this:
Dataset.Tables["tablename"].Columns["columname"].ColumnMapping =
MappingType.Attribute ;

Once you assign all the needed columsn to what they need to be you can
use the relation object to create the "nesting" affect of the XML
document.

so for what you want to do it would like something like this:

ds.Relations.Add(new DataRelation("nameofrelation",
dataset.Tables["tablename"].Columns[parentcolumnindex],
ds.Tables["tablename"].Columns[childcolumnindex]));

So you would want the values for flight number to be the parent of all
the other stuff, and you would then create another relation to make row
and product be part of outofstock.

Then you can set the relation to be nested as i demonstrated above,
this is specifically for formatting the XML document.

What I am suggesting may be way out of context for what you need, and
may be unnecessary to fix what might be a simple problem with what you
already have, I am merely offering a different approach to the
situation.
But i want it to generate this:
<Flight FlightNo="222" LegIndex="1">
<DepartureDate>2006-07-25</DepartureDate>
<Origin>RRR</Origin>
<Destination>GGG</Destination>
<OutOfStock>
<Row>23</Row>
<Product>1</Product>
</OutOfStock>
</Flight>

thanks, maybe this will clarify things!
rhaazy said:
You may be approaching this the wrong way, I recently had a problem
going from a dataset to xml and found the columnmapping property of the
dataset in combination with relation objects to work very well

for example:

ds.Relations.Add(new DataRelation("Appsettings AConfiguration",
ds.Tables["Appsettings"].Columns[0],
ds.Tables["AConfiguration"].Columns[0]));
ds.Relations.Add(new DataRelation("Profile Configuration",
ds.Tables["Profile"].Columns[0],
ds.Tables["Configuration"].Columns[1]));

ds.Relations[0].Nested = true;
ds.Relations[1].Nested = true;

ds.Tables["Appsettings"].Columns["ID"].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[0].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[1].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[2].ColumnMapping =
MappingType.Attribute ;
ds.Tables["AConfiguration"].Columns[3].ColumnMapping =
MappingType.SimpleContent ;
ds.Tables["Profile"].Columns["ID"].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[0].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[1].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[2].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[3].ColumnMapping =
MappingType.Attribute ;
ds.Tables["Configuration"].Columns[4].ColumnMapping =
MappingType.SimpleContent ;

the relation objects create the parent child relationship between the
columns, and the nesting property sets it up for use in your XML
document.
the columnmapping stuff is pretty self explanatory.

Hope this helps.
Help! I created a XML schema with a Visual Studio tools. I'm filling a
dataset with a DataAdapter. Before I use the "WriteXml" method to write the
data to a xml file, I want to map the XSD file I created to the dataset so
that when I do use the "WriteXml" method, the generated xml file will be
properly formatted to the schema I created. I did try using the
"ReadXMLSchema" but it didn't work. Please help me!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top