473,411 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

ReadXML and data types

I have a C# program that reads an XML file into a DataSet set using ReadXml.
I'm trying to get two of the columns in the resulting DataSet to be integer
instead of string. The reaon I'm trying to do this is because I want to
display the DataSet table in a DataGrid with these two columns in currency
format, and it looks like the original data needs to be numeric before I can
convert it to currency. The conversion didn't do anything with a string; it
just displayed it in the original format.

Here is an example of the XML file I'm trying to read (I changed my field
names to be generic):
<?xml version="1.0"?>
<DocElement xmlns:dt="urn:schemas-microsoft-com:datatypes">
<Table1>
<Col1>2</Col1>
<Col2>346</Col2>
<Col3>501</Col3>
<Col4>09/01/2005</Col4>
<Col5>09:51:19</Col5>
<Col6>123456789</Col6>
<Col7 dt:dt="int">211</Col7>
<Col8 dt:dt="int">211</Col8>
<Col9>File1.tif</Col9>
<Col10>File2.tif</Col10>
</Table1>
</DocElement>

When I don't have the dt stuff in the DocElement or Col7 and Col8 lines, I
get a table with 10 columns and all the values as expected. However, when I
read this file with ReadXml, I get a table with only 9 columns when I look at
it in the Visual C# debugger. Either Col7 or Col8 seems to get skipped, and
the one that is remaining has a value of 0 instead of 211. The string values
are still OK.

How can I get this to work? (Or is there a way to convert a numeric string
to currency in a DataGrid?) Thanks -- Stephanie

P.S. Additional notes: At first I tried changing the DataGrid columns to
numeric after reading in the XML (without the dt stuff) into the DataSet and
binding the DataSet table to the DataGrid, but it said that wasn't allowed
since there was already data in the DataSet. Then I was going to try to
predefine my DataSet columns in my app before reading in the XML file, but I
thought there must be a better way. That's when I started trying to set the
data type in the XML file.
--
Stephanie Olds
Retail Professional Services
NCR Corporation
Nov 12 '05 #1
3 2732
Hi Stephanie,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to make a column in a DataSet
Xml Int type. If there is any misunderstanding, please feel free to let me
know.

To achieve this, you cannot simply add an attribute to the element in Xml,
we have to add an inline schema. Here is an example.

<?xml version="1.0" standalone="yes"?>
<DocElement>
<xs:schema id="DocElement" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="DocElement" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Table1">
<xs:complexType>
<xs:sequence>
<xs:element name="Col1" type="xs:string" minOccurs="0" />
<xs:element name="Col2" type="xs:string" minOccurs="0" />
<xs:element name="Col3" type="xs:string" minOccurs="0" />
<xs:element name="Col4" type="xs:string" minOccurs="0" />
<xs:element name="Col5" type="xs:string" minOccurs="0" />
<xs:element name="Col6" type="xs:string" minOccurs="0" />
<xs:element name="Col7" type="xs:int" minOccurs="0" />
<xs:element name="Col8" type="xs:int" minOccurs="0" />
<xs:element name="Col9" type="xs:string" minOccurs="0" />
<xs:element name="Col10" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Table1>
<Col1>2</Col1>
<Col2>346</Col2>
<Col3>501</Col3>
<Col4>09/01/2005</Col4>
<Col5>09:51:19</Col5>
<Col6>123456789</Col6>
<Col7>211</Col7>
<Col8>211</Col8>
<Col9>File1.tif</Col9>
<Col10>File2.tif</Col10>
</Table1>
</DocElement>

You can also seperate the schema into another .xsd file, if you don't want
to put the schema in the xml file.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #2
Kevin,
Thanks for the detailed response -- this was very helpful and my app worked
much better! (Now I just need the 211 to be $2.11, not $211.00, but I'll
work on that.)

If I want to put the schema in a separate XSD file, how do I associate this
with my XML file for ReadXml to use? (I assume I'll have to have some lines
referencing this XSD file in my XML file.) Also, can I have one XSD for
multiple XMLs that have the same format?
--
Stephanie Olds
Retail Professional Services
NCR Corporation
"Kevin Yu [MSFT]" wrote:
Hi Stephanie,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to make a column in a DataSet
Xml Int type. If there is any misunderstanding, please feel free to let me
know.

To achieve this, you cannot simply add an attribute to the element in Xml,
we have to add an inline schema. Here is an example.

<?xml version="1.0" standalone="yes"?>
<DocElement>
<xs:schema id="DocElement" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="DocElement" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Table1">
<xs:complexType>
<xs:sequence>
<xs:element name="Col1" type="xs:string" minOccurs="0" />
<xs:element name="Col2" type="xs:string" minOccurs="0" />
<xs:element name="Col3" type="xs:string" minOccurs="0" />
<xs:element name="Col4" type="xs:string" minOccurs="0" />
<xs:element name="Col5" type="xs:string" minOccurs="0" />
<xs:element name="Col6" type="xs:string" minOccurs="0" />
<xs:element name="Col7" type="xs:int" minOccurs="0" />
<xs:element name="Col8" type="xs:int" minOccurs="0" />
<xs:element name="Col9" type="xs:string" minOccurs="0" />
<xs:element name="Col10" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Table1>
<Col1>2</Col1>
<Col2>346</Col2>
<Col3>501</Col3>
<Col4>09/01/2005</Col4>
<Col5>09:51:19</Col5>
<Col6>123456789</Col6>
<Col7>211</Col7>
<Col8>211</Col8>
<Col9>File1.tif</Col9>
<Col10>File2.tif</Col10>
</Table1>
</DocElement>

You can also seperate the schema into another .xsd file, if you don't want
to put the schema in the xml file.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #3
Hi, Stephanie,

If you want to put the schema in a separate xsd file, you can use
DataSet.ReadXmlSchema to read from the xsd file. The ReadXmlSchema has to
be done before ReadXml that gets data. For example,

ds.ReadXmlSchema("test.xsd");
ds.ReadXml("test.xml");

With this, you don't need to modify your xml to reference the xsd file. And
you can use this xsd file for multiple xml files.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: DraguVaso | last post by:
Hi, I should use XML to synchronize the data from different (VB.NET) applications, and I was just wondering which Overloads of these functions ( ReadXmlSchema, ReadXml and WriteXml) goes the...
1
by: Jeffrey A. Voigt | last post by:
I'm having trouble loading some xml data into a dataset via the ReadXML call. I'm loading into the dataset an xml schema prior to the ReadXML call. I see that there IS in fact, 3 records that...
1
by: mr_dom_is | last post by:
Hi I have a little bit of trouble, probably missing the point now sure but: I have a dataset dim ds as new dataset("User") Which is populated with data from a database. When I use
8
by: Nikhilesh Mehendale | last post by:
I have written a web service in C#, .NET 1.1 which reads a XML file into a dataset. This is just a plain XML file. First I use the Dataset.ReadXmlScheme function and pass the XML file to it. Then...
3
by: Raj Chudasama | last post by:
I am trying to read an xml file and have it view data in the datagrid. (For example take the xml file viewer in the visual studio, when u open an xml file it has that nice readable grid). I...
0
by: mr_dom_is | last post by:
Hi I have a little bit of trouble, probably missing the point not sure but: I have a dataset dim ds as new dataset("User") Which is populated with data from a database. When I use
2
by: C Glenn | last post by:
I'm attempting to use ReadXml. It's working in that I end up with some data in the DataSet. But I'm not able to deal with it effectively after that. The XML file is properly formatted in that it...
0
by: Leeor Chernov | last post by:
Hi , I am using the method: DSSap.ReadXml( XmlPath,XmlReadMode.InferSchema ); And as I expected I get an error when the xml does not matching my DataSet that contains already a schema , The...
10
by: webmaster | last post by:
Getting error: Illegal characters in path what's wrong with this? string xmlSR = "<XmlDS><table1><phone>Value1</phone><name>jake</name></table1></XmlDS>"; myDataSet.ReadXml(xmlSR,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.