READING XML FILE COMPLETELY

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

ATLANTA PATNAIK via DotNetMonster.com

Hi,
Below is my xml file;
<ITPL>
<GATE NAME="2">
<SLOT SNO="5" type="4" status="VACANT"><vehicle slotno="5"
vno="KA12345" vtype="honda" /></SLOT>
<SLOT SNO="6" type="4" status="VACANT"><vehicle slotno="6"
vno="KA12346" vtype="alto" /></SLOT>
<SLOT SNO="7" type="2" status="VACANT"><vehicle slotno="6"
vno="KA12347" vtype="maruti" /></SLOT>
<SLOT SNO="8" type="4" status="VACANT"><vehicle slotno="8"
vtype="scorpio" /></SLOT>
</GATE>
<GATE NAME="1">
<SLOT SNO="1" type="4" status="VACANT"><vehicle slotno="8"
vno="KA12348" vtype="bolero" /></SLOT>
<SLOT SNO="2" type="2" status="VACANT"></SLOT>
<SLOT SNO="3" type="4" status="VACANT"></SLOT>
<SLOT SNO="4" type="2" status="VACANT"></SLOT>
</GATE>
<GATE NAME="3">
<SLOT SNO="9" type="2" status="VACANT"></SLOT>
<SLOT SNO="10" type="2" status="VACANT"></SLOT>
<SLOT SNO="11" type="2" status="VACANT"></SLOT>
<SLOT SNO="12" type="4" status="VACANT"></SLOT>
</GATE>
</ITPL>


How can i display this file completely in c#.net?
 
ATLANTA PATNAIK via DotNetMonster.com said:
Below is my xml file;
<ITPL>
<GATE NAME="2">
<SLOT SNO="5" type="4" status="VACANT"><vehicle slotno="5"
vno="KA12345" vtype="honda" /></SLOT>
<SLOT SNO="6" type="4" status="VACANT"><vehicle slotno="6"
vno="KA12346" vtype="alto" /></SLOT>
<SLOT SNO="7" type="2" status="VACANT"><vehicle slotno="6"
vno="KA12347" vtype="maruti" /></SLOT>
<SLOT SNO="8" type="4" status="VACANT"><vehicle slotno="8"
vtype="scorpio" /></SLOT>
</GATE>
<GATE NAME="1">
<SLOT SNO="1" type="4" status="VACANT"><vehicle slotno="8"
vno="KA12348" vtype="bolero" /></SLOT>
<SLOT SNO="2" type="2" status="VACANT"></SLOT>
<SLOT SNO="3" type="4" status="VACANT"></SLOT>
<SLOT SNO="4" type="2" status="VACANT"></SLOT>
</GATE>
<GATE NAME="3">
<SLOT SNO="9" type="2" status="VACANT"></SLOT>
<SLOT SNO="10" type="2" status="VACANT"></SLOT>
<SLOT SNO="11" type="2" status="VACANT"></SLOT>
<SLOT SNO="12" type="4" status="VACANT"></SLOT>
</GATE>
</ITPL>


How can i display this file completely in c#.net?

What exactly do you mean by "display"? You can load the file using
XmlDocument.Load, but what you do with it afterwards is up to you.
 
Have your caps lock got stuck?

Anyways, I know the answer, but I never help screamers.
You've been a bad puppy!

- Michael S
 
Hi Michael,
By display i mean,reading the xml file indetail.Now can you
tell me as to how can i delete a node within Gate=2/slot=7(if it exists
within that slot).

Regards,
AP
 
ATLANTA PATNAIK via DotNetMonster.com said:
By display i mean,reading the xml file indetail.Now can you
tell me as to how can i delete a node within Gate=2/slot=7(if it exists
within that slot).

Use XPath to find the node, and then remove it from its parent node.
For instance:

using System;
using System.Xml;
using System.Xml.XPath;

class Test
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");

XmlNode node = doc.SelectSingleNode
("//GATE[@NAME='2']/SLOT[@SNO='7']");
if (node != null)
{
node.ParentNode.RemoveChild(node);
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;
doc.WriteTo(writer);
}
else
{
Console.WriteLine ("Couldn't find node");
}
}
}

(where test.xml contains the xml you originally posted) produces:
<ITPL>
<GATE NAME="2">
<SLOT SNO="5" type="4" status="VACANT">
<vehicle slotno="5" vno="KA12345" vtype="honda" />
</SLOT>
<SLOT SNO="6" type="4" status="VACANT">
<vehicle slotno="6" vno="KA12346" vtype="alto" />
</SLOT>
<SLOT SNO="8" type="4" status="VACANT">
<vehicle slotno="8" vtype="scorpio" />
</SLOT>
</GATE>
<GATE NAME="1">
<SLOT SNO="1" type="4" status="VACANT">
<vehicle slotno="8" vno="KA12348" vtype="bolero" />
</SLOT>
<SLOT SNO="2" type="2" status="VACANT">
</SLOT>
<SLOT SNO="3" type="4" status="VACANT">
</SLOT>
<SLOT SNO="4" type="2" status="VACANT">
</SLOT>
</GATE>
<GATE NAME="3">
<SLOT SNO="9" type="2" status="VACANT">
</SLOT>
<SLOT SNO="10" type="2" status="VACANT">
</SLOT>
<SLOT SNO="11" type="2" status="VACANT">
</SLOT>
<SLOT SNO="12" type="4" status="VACANT">
</SLOT>
</GATE>
</ITPL>
 
Back
Top