RELATING C#.Net with XML

  • Thread starter Thread starter ATLANTA PATNAIK via DotNetMonster.com
  • Start date Start date
A

ATLANTA PATNAIK via DotNetMonster.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.
 
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);
 
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);
 
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?
 
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
 
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.
 
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.
 
Back
Top