How does XmlReadMode.Fragment work?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I have a XML Schema (essential elements as follows):
<xs:element name="Accounts">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Account">
<xs:complexType>
<xs:sequence>
<xs:element name="UserId" type="xs:string" />
<xs:element name="Email" type="xs:string" />
....etc...
....and I want to ds.ReadXML, an XML fragment, as follows:
<Account>
<UserId>ACID</UserId>
<Email>[email protected]</Email>
</Account>

....why would the call to ds.ReadXML, skip the contents of the fragment to
the ds?

For more explicit detail...
My problem is contained in the following code that was working, and is now
broken...I have no clue whether it was the xsd or a code changed that
iscausing the XML Fragment to be skipped. It is broken at the "POPULATE THE
DS DATA" section.


// CREATE THE CMD
=====================================================
SqlCommand cmd = cnnPetShop.CreateCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText=
"SELECT UserId,Email FROM Account FOR XML AUTO, ELEMENTS";
cnnPetShop.Open();

// CREATE THE DS
=====================================================
DataSet ds = new DataSet();

ds.WriteXml(@"c:\Account.xml",XmlWriteMode.WriteSchema);
/* Here is the contents of the "c:\Account.xml"
<?xml version="1.0" standalone="yes" ?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded" />
</xs:complexType>
</xs:element>
</xs:schema>
</NewDataSet>
*/

// POPULATE THE DS SCHEMA
=====================================================
ds.ReadXmlSchema(@"Accounts.xsd");

/* Here is the Accounts.xsd
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Accounts"
targetNamespace="http://tempuri.org/Accounts.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/Accounts.xsd"
xmlns:mstns="http://tempuri.org/Accounts.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Accounts">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Account">
<xs:complexType>
<xs:sequence>
<xs:element name="UserId" type="xs:string" />
<xs:element name="Email" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="DocumentKey1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:Account" />
<xs:field xpath="mstns:UserId" />
</xs:unique>
</xs:element>
</xs:schema>
*/

ds.WriteXml(@"c:\Account.xml",XmlWriteMode.WriteSchema);
/* Here is the contents of the "c:\Account.xml"
<?xml version="1.0" standalone="yes" ?>
<Accounts xmlns="http://tempuri.org/Accounts.xsd">
<xs:schema id="Accounts"
targetNamespace="http://tempuri.org/Accounts.xsd"
xmlns:mstns="http://tempuri.org/Accounts.xsd"
xmlns="http://tempuri.org/Accounts.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified"
elementFormDefault="qualified">
<xs:element name="Accounts" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Account">
<xs:complexType>
<xs:sequence>
<xs:element name="UserId" type="xs:string" />
<xs:element name="Email" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="DocumentKey1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:Account" />
<xs:field xpath="mstns:UserId" />
</xs:unique>
</xs:element>
</xs:schema>
</Accounts>
*/


// POPULATE THE DS DATA
//
// THIS IS WHERE IT IS BROKEN BECAUSE THE FRAGMENT NO LONGER FILLS THE DS
WITH DATA
//
======================================================================================
XmlReader xr = cmd.ExecuteXmlReader();
ds.ReadXml(xr,XmlReadMode.Fragment);

/* Here is the fragment's XML
<Account>
<UserId>ACID</UserId>
<Email>[email protected]</Email>
</Account>
*/

ds.WriteXml(@"c:\Account.xml",XmlWriteMode.WriteSchema);
// The contents of c:\Account.xml DO NOT CHANGE AT ALL and incluse only
schema!


// CLEANUP
=====================================================
xr.Close();
cnnPetShop.Close();
 
Back
Top