Click here to Skip to main content
15,897,093 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
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
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<GroupPosts>
  <IndividualPosts>
    <PostID>0</PostID>
    <StudentID>106</StudentID>
    <PostTitle>Math Problems</PostTitle>
    <PostDescription>How Do i study</PostDescription>
  </IndividualPosts>

  <IndividualPosts>
    <PostID>1</PostID>
    <StudentID>0</StudentID>
    <PostTitle>I  have passed</PostTitle>
    <PostDescription>i studied harder</PostDescription>
  </IndividualPosts>

<IndividualPosts>
    <PostID>3</PostID>
    <StudentID>3</StudentID>
    <PostTitle>How to solve math problems</PostTitle>
    <PostDescription>i studied harder</PostDescription>
  </IndividualPosts>


</GroupPosts>




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;
           ds.ReadXml(xmlFile);

           dv = new DataView(ds.Tables[0]);
           dv.Sort = "PostID";
           int index = dv.Find("3");



           listBox1.Items.Add(dv[index]["PostID"].ToString());
           listBox1.Items.Add(dv[index]["StudentID"].ToString());
           listBox1.Items.Add(dv[index]["PostDescription"].ToString());
            listBox1.Items.Add(dv[index]["PostTitle"].ToString());


       }


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
Comments
Shine Ashraf 12-Apr-13 4:48am    
Please try below code after editing the dataset

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

//Saving
ds.AcceptChanges();
xmlFile.Close();
ds.WriteXml("fileName.xml");

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:

XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|*|processing-instruction()|comment()">
    <xsl:copy>
      <xsl:apply-templates select="@*|*|text()|processing-instruction()|comment()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>


[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.]
 
Share this answer
 
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";
            }
            XmlDoc.Save(xmlFileName);

also to know xml and linq operation refer following link..
http://www.dotnetcurry.com/ShowArticle.aspx?ID=564[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900