CF XML Bug?

R

RC

Hi,

here is the source XML file.
data.xml
<table1>
<row1>
<col1>value1</col1>
<col2>value2</col2>
<col3>value3</col3>
</row1>
<row2>
<col1>value4</col1>
<col2>value5</col2>
<col3>value6</col3>
</row2>
<row3>
<col1>value7</col1>
<col2>value8</col2>
<col3>value9</col3>
</row3>
</table1>

to load the XML file into data set

I use following code

Dataset ds = new Dataset();
ds.ReadXML("data.xml");

I now try to delete a row

Dataview dv = ds.DefaultView;
dv.Delete(1);

And then save the dataset back to a new XML file
ds.WriteXML("new.xml");

However, the new.xml will become
<table1>
<row1>
<col1>value1</col1>
<col2>value2</col2>
<col3>value3</col3>
</row1>
</table1>
<row3>
<col1>value7</col1>
<col2>value8</col2>
<col3>value9</col3>
</row3>

Row 3 is not enclosed within table1 tag

Can anybody help me to confirm whether such bug existed?

Thanks
RC
 
I

Ilya Tumanov [MS]

It's not a bug, but a very common case of disagreement between schema
developer had in mind while handcrafting the XML and the actual schema
produced by inference process.
I would suspect you've expected one table called "table1" with columns
"col1", "col2" and "col3" in it. Here's what you'll actually get:

--------------------------- DataSet ----------------------
DataSet: 'table1'
----------------------- Tables -----------------------
Table@0: 'row1'
------------------- Columns ----------------------
Element column@0: 'col1' of 'String' Nullable
Element column@1: 'col2' of 'String' Nullable
Element column@2: 'col3' of 'String' Nullable
----------------- Child Relations ----------------
----------------- Parent Relations ---------------
--------------------- Rows -----------------------
Added: [current: col1=value1, col2=value2, col3=value3]
1 rows in this table
--------------------------------------------------
Table@1: 'row2'
------------------- Columns ----------------------
Element column@0: 'col1' of 'String' Nullable
Element column@1: 'col2' of 'String' Nullable
Element column@2: 'col3' of 'String' Nullable
----------------- Child Relations ----------------
----------------- Parent Relations ---------------
--------------------- Rows -----------------------
Added: [current: col1=value4, col2=value5, col3=value6]
1 rows in this table
--------------------------------------------------
Table@2: 'row3'
------------------- Columns ----------------------
Element column@0: 'col1' of 'String' Nullable
Element column@1: 'col2' of 'String' Nullable
Element column@2: 'col3' of 'String' Nullable
----------------- Child Relations ----------------
----------------- Parent Relations ---------------
--------------------- Rows -----------------------
Added: [current: col1=value7, col2=value8, col3=value9]
1 rows in this table
--------------------------------------------------

The point is: while working with DataSet, _never_ use inference and _never_
handcraft the XML.
Design and load schema and let DataSet to create the XML or be prepared for
a nasty surprises like this one.

To design schema:

1. Add new item to your project.
2. Select XML as item type.
3. Change the name so it would have XSD extension, i.e. myschema.xsd
4. Switch to XML view and add line below to the XML you see:
<xs:schema id="DataSet" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"></xs:schema>
5. Switch to schema view and design your schema.

To add data to XML using designed schema:

1. Add new item to your project.
2. Select XML as item type.
3. Switch to XML view and add the following <DataSet></DataSet>
4. Copy schema XML (without <?xml version="1.0" encoding="utf-8" ?>) and
paste it between <DataSet> and </DataSet>
5. Switch to Data view and enter data (you can also verify if schema you
designed matching your expectations).

Now your XML has embedded schema and inference won't be used.
Or, you can load schema file separately with DataSet.ReadXmlSchema() if you
wish (and you can delete schema from XML in that case).
Make sure schema is loaded before data in that case.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 

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