Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML
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 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 11-Apr-13 23:05pm
Comments
Shine Ashraf at 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");
Rate this: bad
good
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="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.]
  Permalink  
Rate this: bad
good
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";
            }
            XmlDoc.Save(xmlFileName);
also to know xml and linq operation refer following link..
http://www.dotnetcurry.com/ShowArticle.aspx?ID=564[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 245
1 Kamal Rocks 184
2 Sergey Alexandrovich Kryukov 175
3 BillWoodruff 173
4 PIEBALDconsult 160
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 12 Apr 2013
Copyright © CodeProject, 1999-2014
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