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

RELATING C#.Net with XML

Hi,
I have some data from a user,say:Name-X,Age-10.
Can i write a function in C#.Net that would insert this data(taken from the
user at runtime ) into a XML file within a particular node.I want to store
it as:
<student Name="Atlu" Age="10">contents</student>
Thanks & regards,
Miss Patnaik.

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #1
6 2277
There are many ways of doing this, but the most horrible I can think of is:

string userData = "<student name='" + _name + "' age='" + _age +
"'>contents</student>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(userData);
"ATLANTA PATNAIK via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:44******************************@DotNetMonste r.com...
Hi,
I have some data from a user,say:Name-X,Age-10.
Can i write a function in C#.Net that would insert this data(taken from
the
user at runtime ) into a XML file within a particular node.I want to
store
it as:
<student Name="Atlu" Age="10">contents</student>
Thanks & regards,
Miss Patnaik.

--
Message posted via http://www.dotnetmonster.com

Nov 17 '05 #2
Example, where:
"doc" is your XmlDocument instance
"parentNode" is the node under which the new node should be placed.

XmlNode studentNode = doc.CreateNode(XmlNodeType.Element, "student",
string.Empty);
XmlAttribute attr = doc.CreateAttribute("Name");
attr.InnerText = "Atlu";
studentNode.Attributes.Append(attr);
XmlAttribute attr = doc.CreateAttribute("Age");
attr.InnerText = "10";
studentNode.Attributes.Append(attr);

parentNode.AppendChild(studentNode);
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"ATLANTA PATNAIK via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:44******************************@DotNetMonste r.com...
Hi,
I have some data from a user,say:Name-X,Age-10.
Can i write a function in C#.Net that would insert this data(taken from
the
user at runtime ) into a XML file within a particular node.I want to
store
it as:
<student Name="Atlu" Age="10">contents</student>
Thanks & regards,
Miss Patnaik.

--
Message posted via http://www.dotnetmonster.com

Nov 17 '05 #3
my XML FILE:<ITPL>
<GATE NAME='2'>
<Slot sno='5'>slot no=5, type=4,VACANT</Slot>
<Slot sno='6'>slot no=6, type=4,VACANT</Slot>
<Slot sno='7'>slot no=7, type=2,VACANT</Slot>
<Slot sno='8'>slot no=8, type=4,VACANT</Slot>
</GATE>
<GATE NAME='1'>
<Slot sno='1'>slot no=1, type=4,VACANT</Slot>
<Slot sno='2'>slot no=2, type=2,VACANT</Slot>
<Slot sno='3'>slot no=3, type=4,VACANT</Slot>
<Slot sno='4'>slot no=4, type=2,VACANT</Slot>
</GATE>
</ITPL>:
I want to insert a new childnode under slot=5,which has its own
attributes.The code i have used is:

XmlDocument doc = new XmlDocument();
doc.Load ("c:\\gates.xml");
XmlNode Slot = doc.DocumentElement;
XmlNode vehicleNode = doc.CreateNode(XmlNodeType.Element, "vehicle",
string.Empty);
XmlAttribute attr = doc.CreateAttribute("slotno");
attr.InnerText = "5";
vehicleNode.Attributes.Append(attr);
XmlAttribute attr1 = doc.CreateAttribute("vno");
attr.InnerText = "KA12345";
vehicleNode.Attributes.Append(attr);
XmlAttribute attr2 = doc.CreateAttribute("vtype");
attr.InnerText = "honda";
vehicleNode.Attributes.Append(attr);

Slot.AppendChild(vehicleNode);
I want to know as how to query that particular slot before inserting the
new node?

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #4
You can use the SelectSingleNode() method providing an XPath expression to
select to node. Please take in mind that the casing in the XPath query needs
to be the same as in the XML document, therefore it might be a good practice
to always use lower- or uppercasing:

XmlDocument doc = new XmlDocument();
doc.Load(@"c:\gates.xml");

XmlNode root = doc.DocumentElement;

XmlNode foundNode;
foundNode=root.SelectSingleNode("GATE[@NAME='2']/Slot[@sno='5']");

XmlNode vehicleNode = doc.CreateNode(XmlNodeType.Element, "vehicle",
string.Empty);
XmlAttribute attr = doc.CreateAttribute("slotno");
attr.InnerText = "5";
vehicleNode.Attributes.Append(attr);
XmlAttribute attr1 = doc.CreateAttribute("vno");
attr.InnerText = "KA12345";
vehicleNode.Attributes.Append(attr);
XmlAttribute attr2 = doc.CreateAttribute("vtype");
attr.InnerText = "honda";
vehicleNode.Attributes.Append(attr);

foundNode.AppendChild(vehicleNode);

Gabriel Lozano-Morán

"ATLANTA PATNAIK via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
message news:b9******************************@DotNetMonste r.com...
my XML FILE:<ITPL>
<GATE NAME='2'>
<Slot sno='5'>slot no=5, type=4,VACANT</Slot>
<Slot sno='6'>slot no=6, type=4,VACANT</Slot>
<Slot sno='7'>slot no=7, type=2,VACANT</Slot>
<Slot sno='8'>slot no=8, type=4,VACANT</Slot>
</GATE>
<GATE NAME='1'>
<Slot sno='1'>slot no=1, type=4,VACANT</Slot>
<Slot sno='2'>slot no=2, type=2,VACANT</Slot>
<Slot sno='3'>slot no=3, type=4,VACANT</Slot>
<Slot sno='4'>slot no=4, type=2,VACANT</Slot>
</GATE>
</ITPL>:
I want to insert a new childnode under slot=5,which has its own
attributes.The code i have used is:

XmlDocument doc = new XmlDocument();
doc.Load ("c:\\gates.xml");
XmlNode Slot = doc.DocumentElement;
XmlNode vehicleNode = doc.CreateNode(XmlNodeType.Element, "vehicle",
string.Empty);
XmlAttribute attr = doc.CreateAttribute("slotno");
attr.InnerText = "5";
vehicleNode.Attributes.Append(attr);
XmlAttribute attr1 = doc.CreateAttribute("vno");
attr.InnerText = "KA12345";
vehicleNode.Attributes.Append(attr);
XmlAttribute attr2 = doc.CreateAttribute("vtype");
attr.InnerText = "honda";
vehicleNode.Attributes.Append(attr);

Slot.AppendChild(vehicleNode);
I want to know as how to query that particular slot before inserting the
new node?

--
Message posted via http://www.dotnetmonster.com

Nov 17 '05 #5
Thanks.

Now can you tell as to how can i display the entire XML file.Also tell as
how to delete that vehiclenode (the one we had inserted under slot=5).

Regards,
Patnaik.

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #6
I have used the above code but my XML file is showing the below tag:

<vehicle slotno="honda" /><vehicle slotno="honda" vno="" vtype="" />

Why slotno is having value of vtype and why do vno and vtype is not taking
any value at all.

Thanks.

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #7

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

Similar topics

6
by: Jon Bosker | last post by:
Help! This is probably easy but I just don't get it. I am trying to relate and merge 2 datasets. My XML file has 2 datasets (1st level nodes) TimeDetail and TimeSummary. The report is supposed to...
2
by: Chipmunk | last post by:
I am currently developing a website (ASP.NET) which allows users to submit a web form containing a href link in one field and descriptive text in another field. The records will stored to varchar...
1
by: Mitchell Vincent | last post by:
I'm writing some business software, so I have a customer object that holds customer data and all the logic to deal with that customer. The problem is one of concept - I don't know how to tie the...
1
by: c.anandkumar | last post by:
Hi How do I figure out if a selection in a document is in a particular control? I have a TEXTAREA in which a user can type in some text, select some text and 'apply' tags around it clicking a...
2
by: John Walton | last post by:
Hello, again. I'm back with my instant messenger project. My teacher has assigned us to write our papers, excluding the procedure, results, and conclusion. One of my topics is going to be...
3
by: BlackFireNova | last post by:
I have an existing database, and I need to add another table to it. The database tracks equipment, however I have a need to track ancillary items which are purchased or added to some of the...
3
by: Stephen | last post by:
It is possible to relate queries to tables, right? It seems logical but when I try to match my queries to any of the tables or even to each other it gives me a blank relationship. What could I...
2
by: Beeeeeves | last post by:
Hi I am looking for any articles anyone may know of on developing a finite state machine in a gui application? Basically what I have is a fairly complex gui which needs to go through several (3 -...
3
by: qwerty | last post by:
I´m new to ASP.Net. My workmate has some experience with it. He claimed that in ASP.Net working with frames is much simpler than it was ASP. I asked explanation but he couldn't give me such. (a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.