473,322 Members | 1,714 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,322 software developers and data experts.

Read & Write To XML

Hi,

i like to read an existing xml file which has a schema defined to it, and
then write or add data to the existing xml file using vb.net/c#.

May be this Question has been answered earlier.
Pls if anyone knows the link or example let me know

Thanks
ARvind.
Nov 11 '05 #1
8 6344
Ice
did you search the archives?

ice
"Arvind P Rangan" <ar******@hotmail.com> wrote in message
news:eH*************@tk2msftngp13.phx.gbl...
Hi,

i like to read an existing xml file which has a schema defined to it, and
then write or add data to the existing xml file using vb.net/c#.

May be this Question has been answered earlier.
Pls if anyone knows the link or example let me know

Thanks
ARvind.

Nov 11 '05 #2
Arvind P Rangan wrote:
i like to read an existing xml file which has a schema defined to it, and
then write or add data to the existing xml file using vb.net/c#.


MSDN is your friend:
"Reading an XML Document into the DOM" [1]
"Inserting Nodes into an XML Document" [2]
et cetera

[1]
http://msdn.microsoft.com/library/de...ltransform.asp
[2]
http://msdn.microsoft.com/library/de...ltransform.asp
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #3
I'm trying to do the same. I've managed to open the xml,
updated/appended to it and saved it. The resulting
document now fails schema validation as any new fields
are not added to document in the correct sequence.

I've tried reading the xml into a dataset, but that also
corrupts any xml fields of type xs:gYearMonth or
xs:dateTime.

Does anyone have a solution?

Oliver
-----Original Message-----
Arvind P Rangan wrote:
i like to read an existing xml file which has a schema defined to it, and then write or add data to the existing xml file using
vb.net/c#.
MSDN is your friend:
"Reading an XML Document into the DOM" [1]
"Inserting Nodes into an XML Document" [2]
et cetera

[1]
http://msdn.microsoft.com/library/default.asp? url=/library/en-
us/cpguide/html/cpconxmldocumentinputtoxsltransform.asp[2]
http://msdn.microsoft.com/library/default.asp? url=/library/en-
us/cpguide/html/cpconxmldocumentinputtoxsltransform.asp--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

.

Nov 11 '05 #4
Oliver Tamayo wrote:
I'm trying to do the same. I've managed to open the xml,
updated/appended to it and saved it. The resulting
document now fails schema validation as any new fields
are not added to document in the correct sequence.

So you must be doing something wrong, right?
Show us your code and expalain what are you trying to achieve.

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #5
Hi Oleg,
THanks for the links,
But both the links are opening the reading method, can u resend the link for
Inserting Nodes into XML Document.

Thanks
ARvind
"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:eF**************@TK2MSFTNGP09.phx.gbl...
Arvind P Rangan wrote:
i like to read an existing xml file which has a schema defined to it, and then write or add data to the existing xml file using vb.net/c#.
MSDN is your friend:
"Reading an XML Document into the DOM" [1]
"Inserting Nodes into an XML Document" [2]
et cetera

[1]

http://msdn.microsoft.com/library/de...ltransform.asp [2]
http://msdn.microsoft.com/library/de...ltransform.asp --
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #6
Arvind P Rangan wrote:
THanks for the links,
But both the links are opening the reading method, can u resend the link for
Inserting Nodes into XML Document.


Sure, here it is:
http://msdn.microsoft.com/library/de...mldocument.asp

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #7
Oleg,

Save the following as XMLSchema.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="XMLSchema" targetNamespace="XMLSchema.xsd"
elementFormDefault="qualified" xmlns="XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="customerdetails">
<xs:sequence>
<xs:element name="name"
type="xs:string" />
<xs:element name="surname"
type="xs:string" />
<xs:element name="addressline1"
type="xs:string" />
<xs:element name="addressline2"
type="xs:string" minOccurs="0" />
<xs:element name="addressline3"
type="xs:string" minOccurs="0" />
<xs:element name="postalcode"
type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="orders">
<xs:complexType>
<xs:sequence>
<xs:element name="order"
minOccurs="1" maxOccurs="unbounded">
<xs:complexType>

<xs:sequence>

<xs:element name="orderid" type="xs:string" />

<xs:element name="orderdate" type="xs:dateTime"
minOccurs="0" />

<xs:element name="customer"
type="customerdetails" minOccurs="1" />

<xs:element name="shipto" type="customerdetails"
minOccurs="0" />

<xs:element name="value" type="xs:decimal" />

<xs:element name="expirydate"
type="xs:gYearMonth" />

</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Next save this as sample.xml

<?xml version="1.0" encoding="utf-8" ?>
<orders xmlns="XMLSchema.xsd">
<order>
<orderid>12345</orderid>
<customer>
<name>Joe</name>
<surname>Bloggs</surname>

<addressline1>Somewhere</addressline1>
<postalcode>AB123CD</postalcode>
</customer>
<value>10.99</value>
<expirydate>2005-03</expirydate>
</order>
</orders>

Finally, run the following piece of VB.NET.

Public Sub Main()

Dim ds As New DataSet
Dim dt As DataTable
Dim row As DataRow
Dim xmlDoc As XmlDataDocument

' Update the document using a set
ds.ReadXmlSchema("..\XMLSchema.xsd")
xmlDoc = New XmlDataDocument(ds)
xmlDoc.Load("..\Sample.xml")

' Add the shipping address
dt = ds.Tables("shipto")
row = dt.NewRow
row!order_id = ds.Tables("order").Select
("orderid='12345'")(0)!order_id
row!name = "John"
row!surname = "Doe"
row!addressline1 = "Somewhere"
row!postalcode = "ABC1234"
dt.Rows.Add(row)

xmlDoc.Save("c:\temp\updatedbydataset.xml")

End Sub

The output file, c:\temp\updatedbydataset.xml will not
pass schema validation as the new shipping address is
added to wrong part of the document.

Modifing the expirydate field will also cause the
document to fail validation.

Regards

Oliver

-----Original Message-----
Oliver Tamayo wrote:
I'm trying to do the same. I've managed to open the xml, updated/appended to it and saved it. The resulting
document now fails schema validation as any new fields
are not added to document in the correct sequence.So you must be doing something wrong, right?
Show us your code and expalain what are you trying to

achieve.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

.

Nov 11 '05 #8
I believe you are looking for an XmlDocument that validates against the
schema as you modify the DOM. Unfortunately it doesn't exist in the .Net
class library, you would need to write your own implementation that inherits
from System.Xml.XmlDocument and XmlElement etc to validate against the
schema. You would also need to provide access to the schema types relevant
to each element, attribute etc so that you can programatically determine the
correct content to use.

Colin

"Oliver Tamayo" <ol*****************@hotmail.com> wrote in message
news:28*****************************@phx.gbl...
Oleg,

Save the following as XMLSchema.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="XMLSchema" targetNamespace="XMLSchema.xsd"
elementFormDefault="qualified" xmlns="XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="customerdetails">
<xs:sequence>
<xs:element name="name"
type="xs:string" />
<xs:element name="surname"
type="xs:string" />
<xs:element name="addressline1"
type="xs:string" />
<xs:element name="addressline2"
type="xs:string" minOccurs="0" />
<xs:element name="addressline3"
type="xs:string" minOccurs="0" />
<xs:element name="postalcode"
type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="orders">
<xs:complexType>
<xs:sequence>
<xs:element name="order"
minOccurs="1" maxOccurs="unbounded">
<xs:complexType>

<xs:sequence>

<xs:element name="orderid" type="xs:string" />

<xs:element name="orderdate" type="xs:dateTime"
minOccurs="0" />

<xs:element name="customer"
type="customerdetails" minOccurs="1" />

<xs:element name="shipto" type="customerdetails"
minOccurs="0" />

<xs:element name="value" type="xs:decimal" />

<xs:element name="expirydate"
type="xs:gYearMonth" />

</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Next save this as sample.xml

<?xml version="1.0" encoding="utf-8" ?>
<orders xmlns="XMLSchema.xsd">
<order>
<orderid>12345</orderid>
<customer>
<name>Joe</name>
<surname>Bloggs</surname>

<addressline1>Somewhere</addressline1>
<postalcode>AB123CD</postalcode>
</customer>
<value>10.99</value>
<expirydate>2005-03</expirydate>
</order>
</orders>

Finally, run the following piece of VB.NET.

Public Sub Main()

Dim ds As New DataSet
Dim dt As DataTable
Dim row As DataRow
Dim xmlDoc As XmlDataDocument

' Update the document using a set
ds.ReadXmlSchema("..\XMLSchema.xsd")
xmlDoc = New XmlDataDocument(ds)
xmlDoc.Load("..\Sample.xml")

' Add the shipping address
dt = ds.Tables("shipto")
row = dt.NewRow
row!order_id = ds.Tables("order").Select
("orderid='12345'")(0)!order_id
row!name = "John"
row!surname = "Doe"
row!addressline1 = "Somewhere"
row!postalcode = "ABC1234"
dt.Rows.Add(row)

xmlDoc.Save("c:\temp\updatedbydataset.xml")

End Sub

The output file, c:\temp\updatedbydataset.xml will not
pass schema validation as the new shipping address is
added to wrong part of the document.

Modifing the expirydate field will also cause the
document to fail validation.

Regards

Oliver

-----Original Message-----
Oliver Tamayo wrote:
I'm trying to do the same. I've managed to open the xml, updated/appended to it and saved it. The resulting
document now fails schema validation as any new fields
are not added to document in the correct sequence.

So you must be doing something wrong, right?
Show us your code and expalain what are you trying to

achieve.

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

.

Nov 11 '05 #9

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

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
0
by: Abubakar | last post by:
Hi, try { int x = ns.Read(readbuffer, 0, readbuffer.Length); } catch (System.IO.IOException ioexception) { UINotifications.ServerMessageDisplay(ioexception.ToString( )); }
5
by: Arvind P Rangan | last post by:
Hi, i like to read an existing xml file which has a schema defined to it, and then write or add data to the existing xml file using vb.net/c#. May be this Question has been answered earlier....
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
6
by: cj | last post by:
I'm receiving an xml formatted string that I pull data from by reading it into an xml document like this: Dim doc As New Xml.XmlDocument doc.LoadXml(respstr) Dim co_name As Xml.XmlNodeList =...
0
by: rchadda | last post by:
I haven't started using Seqouia/C-jdbc but I am probing the ability to do something like this: Controller CW has 2 controllers under it - C1 & C2. C1 is local and has a host HA with X.db's...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
0
by: magicbeans | last post by:
I am trying to create a .bin file of records from a .txt file. Initially the records are stored in a text file and I want to Read from the .txt file and Write to the .bin file. This is the C++...
2
by: Bina | last post by:
hi, I want to read & write a html file which contain the japanese character. Now how i will do it?I can read & write english character . but when i read & write Japanese character from the html...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.