Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XmlTextWriter nesting tables

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
US
I'm using XmlTextWriter (.NET) to create an xml file for a client to read orders into their system when they process an order.

Obviously, most of the time there will be more than one items in the order so I will have to loop through the OrderDetails table for a given Order and repeat a certain part of the XML file for each record.

Several records should go like this, at least according to the sample documentation that I got.

for example:

Code:
<Product>
  <LineItem>123</LineItem>
  <Quantity>1</Quantity>
  ....
</Product>
<Product>
  <LineItem>456</LineItem>
  <Quantity>1</Quantity>
  ....
</Product>
<Product>
  <LineItem>890</LineItem>
  <Quantity>1</Quantity>
  ....
</Product>

But what happens is that instead of putting them like this it nests them -- which I'm not sure is correct or not:

Code:
<Product>
  <LineItem>123</LineItem>
  <Quantity>1</Quantity>
  ....
<Product>
  <LineItem>456</LineItem>
  <Quantity>1</Quantity>
  ....
<Product>
  <LineItem>890</LineItem>
  <Quantity>1</Quantity>
  ....
</Product>
</Product>
</Product>

VS.NET gives me this message when I try to display it in the data view mode of the xml editor:

Although this XML document is well formed, it contains structure that the Data View cannot display. The same table (Product) cannot be the child table in two nested relations.

If I leave out the WriteEndElement() at the end of the loop (WriteStartElement("Product") is at the beginning), it just goes ahead and nests the tables any way, just as if the WriteEndElement() was there.

Are both of these correct? Will my client be able to read the data if it is nested like that? If not how can I correct it so that it doesn't nest the tables?

Thanks for the help.
 
I dont know exactly where your problem is but this is actually printing the corrcet structure!
Code:
XmlTextWriter myWriter = new XmlTextWriter(filepath,System.Text.Encoding.Default);
myWriter.WriteStartElement("Root");
for(int x =0; x<5;x++)
{
    myWriter.WriteStartElement("Product");

    myWriter.WriteStartElement("LineItem");
    myWriter.WriteRaw("123");
    myWriter.WriteEndElement();

    myWriter.WriteStartElement("Quantity");
    myWriter.WriteRaw("1");
    myWriter.WriteEndElement();
    myWriter.WriteEndElement();
}
myWriter.WriteEndElement();
myWriter.Flush();
myWriter.Close();

Or did I understand your question wrong?

Stephan
 
Oh by the way heres the Xml it printed:
Code:
<Root>
	<Product>
		<LineItem>123</LineItem>
		<Quantity>1</Quantity>
	</Product>
	<Product>
		<LineItem>123</LineItem>
		<Quantity>1</Quantity>
	</Product>
	<Product>
		<LineItem>123</LineItem>
		<Quantity>1</Quantity>
	</Product>
	<Product>
		<LineItem>123</LineItem>
		<Quantity>1</Quantity>
	</Product>
	<Product>
		<LineItem>123</LineItem>
		<Quantity>1</Quantity>
	</Product>
</Root>

And it needs to be this structure! Your customer most likely will not understand the other document.

If you have any other questions just let me know!

Stephan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top