How to delete a specified node while working on xml with asp.net? [Resolved]

Posted by ttarakarajesh-8732 under ASP.NET on 4/15/2011 | Points: 10 | Views : 4662 | Status : [Member] | Replies : 9
Hi Everone,

I have an xml file as follows. Can anybody tell how to delete Allen record from the following file.?

<Employee>
- <Emp>
<ENo>101</ENo>
<EName>Narendra</EName>
<Job>ASE</Job>
<Sal>5000</Sal>
</Emp>
- <Emp>
<ENo>jdkljf</ENo>
<EName>DKLFJ</EName>
<Job>DJKLAJ</Job>
<Sal>KJDKLFJ</Sal>
</Emp>
- <Emp>
<ENo>DJFJA</ENo>
<EName>JKLJKLJ</EName>
<Job>JKLJKL</Job>
<Sal>JKLJ</Sal>
</Emp>
- <Emp>
<ENo>103</ENo>
<EName>Allen</EName>
<Job>Accounts</Job>
<Sal>2500</Sal>
</Emp>
</Employee>

I Used the following code and found Error: Object reference not set to an instance of an object.
And observed that ParentNode is null when i use the following code..

XmlDocument doc=new XmlDocument();
doc.Load(Server.MapPath(@"~/App_Data/XmlFile.xml""));
XmlNode node = doc.SelectSingleNode("//EName[@" + txtEName.Text + "]");
doc.ParentNode.RemoveChild(node);

U can mail to ttarakarajesh@gmail.com, if u have solution.

ThanQ
Tarak




Responses

Posted by: Ndebata on: 4/20/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi

Please take a look at the attached code sample built in 2.0 framework.

Hope that can answer your query.

Thanks,
Debata
 Download source file

ttarakarajesh-8732, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Umeshdwivedi on: 4/15/2011 [Member] Starter | Points: 25

Up
0
Down
Hello,
Follow the code given below.
Change according to your requirements.
Tat's it !!!
Take care.

Dim LOC_XML_Document As New XmlDocument
Dim LOC_NodeItem As XmlNode
Dim LOC_RootElement As XmlElement
Dim LOC_IEnum As IEnumerator
Dim LOC_XMLNodeList As XmlNodeList
LOC_XML_Document.Load(FileName)
LOC_RootElement = LOC_XML_Document.DocumentElement
LOC_XMLNodeList = LOC_RootElement.GetElementsByTagName("Name of element tag")
LOC_IEnum = LOC_XMLNodeList.GetEnumerator()
While LOC_IEnum.MoveNext
if "Condition"
LOC_NodeItem = CType(LOC_IEnum.Current, XmlNode)
'This may differ accoding to your requirement LOC_NodeItem.ParentNode.ParentNode.RemoveChild(LOC_NodeItem.ParentNode)
Exit While
end if
End While
LOC_XML_Document.Save(FileName)

Latest Technology Trainer
And Part time software consultant

ttarakarajesh-8732, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Umeshdwivedi on: 4/15/2011 [Member] Starter | Points: 25

Up
0
Down
hello
i find same articals visit this link i hope you understand
http://www.andreas-kraus.net/blog/deleting-specific-attributes-in-a-xml-file-aspnet/

Latest Technology Trainer
And Part time software consultant

ttarakarajesh-8732, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
The following code creates a sample xml.
I am using the namespace
"using System.Xml.Linq;"
           XElement xe = new XElement

("Students",
new XElement("Student", new XAttribute("ID", 1), new XElement("Name", "Amir")),
new XElement("Student", new XAttribute("ID", 2), new XElement("Name", "Katrina")),
new XElement("Student", new XAttribute("ID", 3), new XElement("Name", "Salman"))
);

let say you want to delete The Student whose ID is 3 , in this case Salman
            var snode=xe.Elements().Where(e => e.Attribute("ID").Value == "3").FirstOrDefault();

if(snode!=null)
snode.Remove();


Thanks,
Debata

ttarakarajesh-8732, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Rajesh,

In your case the code can be modified as

XElement xe = XElement.Load(Server.MapPath(@"~/App_Data/XmlFile.xml"")); 

var snode = xe.Elements().Where(e => e.Element("EName").Value.Equals("Allen", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (snode != null)
snode.Remove();


Thanks,
Debata

ttarakarajesh-8732, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ttarakarajesh-8732 on: 4/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Debata,
First of all thanks for your answer. In my scenario i use XML but not LINQ. I'm developing asp.net application with c#.

ttarakarajesh-8732, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Rajesh,

May I know which version of DOT.Net FrameWork you are using.
If it is 3.5 or more , just include the namespace thats it.

Thanks,
Debata

ttarakarajesh-8732, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ttarakarajesh-8732 on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Debata,
I use .NET2.0. And could you tell me how to use gridview control's events(rowediting event) over xml?

Thanks,
Taraka Rajesh

ttarakarajesh-8732, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ttarakarajesh-8732 on: 4/21/2011 [Member] Starter | Points: 25

Up
0
Down
Dear Debata,
Thanks for your help. Your code answered my query. Can you tell me what things should i keep in mind while programming?

Regards,
Taraka Rajesh

ttarakarajesh-8732, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response