XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Yama KamyarSubject: Accessing a Dataset
Author: Yama Kamyar
Date: 21 Mar 2005 07:08 PM
Hello,

I have the following XML (ADO like) data:

<?xml version="1.0" encoding="UTF-16"?>
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly" rs:CommandTimeout="30">
<s:AttributeType name="CustomerID" rs:number="1" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="5" rs:fixedlength="true" rs:maybenull="false" /></s:AttributeType><s:AttributeType name="CompanyName" rs:number="2" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="40" rs:maybenull="false" /></s:AttributeType><s:AttributeType name="ContactName" rs:number="3" rs:nullable="true" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="30" /></s:AttributeType><s:AttributeType name="ContactTitle" rs:number="4" rs:nullable="true" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="30" /></s:AttributeType><s:AttributeType name="Address" rs:number="5" rs:nullable="true" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="60" /></s:AttributeType><s:AttributeType name="City" rs:number="6" rs:nullable="true" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="15" /></s:AttributeType><s:AttributeType name="Region" rs:number="7" rs:nullable="true" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="15" /></s:AttributeType><s:AttributeType name="PostalCode" rs:number="8" rs:nullable="true" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="10" /></s:AttributeType><s:AttributeType name="Country" rs:number="9" rs:nullable="true" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="15" /></s:AttributeType><s:AttributeType name="Phone" rs:number="10" rs:nullable="true" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="24" /></s:AttributeType><s:AttributeType name="Fax" rs:number="11" rs:nullable="true" rs:writeunknown="true"><s:datatype dt:type="string" dt:maxLength="24" /></s:AttributeType><s:extends type="rs:rowbase" /></s:ElementType></s:Schema><rs:data><z:row CustomerID="ALFKI" CompanyName="Alfreds Futterkiste" ContactName="Maria Anders" ContactTitle="Sales Representative" Address="Obere Str. 57" City="Berlin" PostalCode="12209" Country="Germany" Phone="030-0074321" Fax="030-0076545" /><z:row CustomerID="ANATR" CompanyName="Ana Trujillo Emparedados y helados" ContactName="Ana Trujillo" ContactTitle="Owner" Address="Avda. de la Constitución 2222" City="México D.F." PostalCode="05021" Country="Mexico" Phone="(5) 555-4729" Fax="(5) 555-3745" /></rs:data></xml>

Here is some code I use to load the data into a dataset:

XmlTextReader xmlReader = new XmlTextReader(stream);
XmlDocument _doc = new XmlDocument();
XslTransform _transform = new XslTransform();
_doc.Load(xmlReader);
DataSet ds = new DataSet();
ds.ReadXml(stream, XmlReadMode.InferSchema);
ds.Namespace = "#RowsetSchema";

When I use ds.GetXml(); I get the following:
<xml xmlns="#RowsetSchema">
<rs:data xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row CustomerID="ALFKI" CompanyName="Alfreds Futterkiste" ContactName="Maria Anders" ContactTitle="Sales Representative" Address="Obere Str. 57" City="Berlin" PostalCode="12209" Country="Germany" Phone="030-0074321" Fax="030-0076545" xmlns:z="#RowsetSchema" />
<z:row CustomerID="ANATR" CompanyName="Ana Trujillo Emparedados y helados" ContactName="Ana Trujillo" ContactTitle="Owner" Address="Avda. de la Constitución 2222" City="México D.F." PostalCode="05021" Country="Mexico" Phone="(5) 555-4729" Fax="(5) 555-3745" xmlns:z="#RowsetSchema" />
</rs:data>
</xml>

Everything is great and all but I can't seem to access the columns. I would like to format some column names (i.e. "ContactName" to "Contact Name"). Also I would like to append or format some values for example change every instance PostalCode to include an additional "-0000" at the end.

Any clues?

Thanks,

~yamazed

Postnext
Ivan PedruzziSubject: Accessing a Dataset
Author: Ivan Pedruzzi
Date: 24 Mar 2005 12:02 AM
Yamazed,


Out of curiosity is the stream object pointing to an XML file?
If yes why are you using a DataSet and not just load the file into an XmlDocument object?

XML element name and attribute name can't contain space.


Ivan

Postnext
Yama KamyarSubject: Accessing a Dataset
Author: Yama Kamyar
Date: 24 Mar 2005 03:51 AM
Hi,

I resolve this issue but I get an output XML with that freaking hex _x0020_ for space! Now trying to figure out the best way to parse it.

Thanks,

~yamazed

Posttop
Yama KamyarSubject: Accessing a Dataset
Author: Yama Kamyar
Date: 24 Mar 2005 12:31 PM
Hi,

For those of you interested here is how I accessed the loaded dataset using ds.ReadXml(stream, XmlReadMode.InferSchema).

/// <summary>
/// Format Dataset:
/// Format Column names where necessary
/// <remark>
/// could retrieve from a config file then loop
/// through and change column names
/// </remark>
/// Convert, parse, or map the row to appropriate values
/// </summary>
/// <param name="ds">
/// ds.ReadXml(stream, XmlReadMode.InferSchema)
/// stream = well formed XML data in MemoryStream
/// </param>
private void PrintValues(DataSet ds)
{
Debug.Write("\r\n" + "TableName: ");
foreach(DataTable dt in ds.Tables)
{
Debug.Write("TableName: " + dt.TableName);
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dt.Columns)
{
switch(dc.ColumnName)
{
case "ColumnName1":
dc.ColumnName = "Column Name 1";
break;
case "ColumnName2":
dc.ColumnName = "Column Name 2";
Convert.ToInt64(dr[dc]));
break;
// etc...
}
Debug.Write("\t " + dc.ColumnName + ": " + dr[dc]);
}
Debug.WriteLine("");
ds.AcceptChanges();
}
}
}


Yama

 
Topic Page 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.