Click here to Skip to main content
13,000,324 members (74,670 online)
Rate this:
Please Sign up or sign in to vote.
See more:

Can anyone help me out with how to search through an xml file and then edit the results, for example here is my how my xml file looks like below :

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <PostTitle>Math Problems</PostTitle>
    <PostDescription>How Do i study</PostDescription>
    <PostTitle>I  have passed</PostTitle>
    <PostDescription>i studied harder</PostDescription>
    <PostTitle>How to solve math problems</PostTitle>
    <PostDescription>i studied harder</PostDescription>


What I want to do is this, search through the file using the postId element, retrieve the results and and the edit those results and save the changes to that file. The searching part i have no problem. But how do I edit the search results and save the changes to that file. Here is my code for searching:

 public  void getPost(string grpName, int i)
            XmlReader xmlFile;
            xmlFile = XmlReader.Create("fileName.xml", new XmlReaderSettings());
            DataSet ds = new DataSet();
            DataView dv;
            dv = new DataView(ds.Tables[0]);
            dv.Sort = "PostID";
            int index = dv.Find("3");


How do I edit the results save the changes to that same file and how do I go about it, if anyone were to provide me with useful links that might help me I would really appreciate it. Thanks in advance
Posted 11-Apr-13 22:05pm
Shine Ashraf 12-Apr-13 4:48am
Please try below code after editing the dataset

int index = dv.Find("3");
dv[index]["PostTitle"] = "Edited";

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You want to transform the xml using xsl/xslt and perform the identity transform on all data except for the data you want to change. Add an xslt rule for the data you want changed and (duh) make the change there.

The identity transform looks like:

<xsl:stylesheet version="1.0" xmlns:xsl="">
  <xsl:template match="@*|*|processing-instruction()|comment()">
      <xsl:apply-templates select="@*|*|text()|processing-instruction()|comment()"/>

[The only things this transform will not preserve are formatting, white space or CDATA but that does not seem to be an issue for the data you present here.]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

it will be easy using linq try this code before add reference
using System.Xml.Linq;

string xmlFileName = @"fileName.xml";
            XDocument XmlDoc = XDocument.Load(xmlFileName);
            List<XElement> XmlElements = (from fx in XmlDoc.Root.DescendantsAndSelf("IndividualPosts") where (((fx.Element("PostID").Value == "3")) select fx).ToList();
            foreach (XElement gg in XmlElements)
                gg.Element("PostID").Value = "addNewValue1";
                gg.Element("StudentID").Value = "addNewValue2";
                gg.Element("PostDescription").Value = "addNewValue3";
                gg.Element("PostTitle").Value = "addNewValue4";

also to know xml and linq operation refer following link..[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 12 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100