lundi 30 juillet 2018

Unable to display edited value

I am working on writing and reading XML data using LINQ. The reading part is okay but the wirting part is not working.

The post data from my xml is loading properly, but as soon the user trying to save new changes, the data remain unchange.Wonder why ? enter image description here

Reading post data :

public partial class EditPost : System.Web.UI.Page
{
    private void Page_Load(object sender, EventArgs e)
    {

        string new_title = newtitle.Text.ToString();
        string new_description = update_des.Value.ToString();

        string path = "C:\\Users\\user\\Source\\Repos\\FoodBlog\\FoodBlog\\Data\\blog_post.xml";
        string postid = Request.QueryString["pid"];





        if (postid != null)
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            XDocument xdoc = XDocument.Load(fs);
            var post = from p in xdoc.Descendants("post")
                       select new
                       {
                           Title = p.Element("title"),
                           Subtitle = p.Element("subtitle"),
                           Desc = p.Element("description"),
                           PID = p.Element("pid")

                       };
            foreach (var ePosts in post)
            {
                if (ePosts.PID.Value == postid)
                {
                    newtitle.Text = ePosts.Title.Value;
                    Subtitle.Value = ePosts.Subtitle.Value;
                    update_des.Value = ePosts.Desc.Value;
                }
            }

        }


    }

Writing post data :

protected void Update_btn_click(object sender, EventArgs e)
    {
        string postid = Request.QueryString["pid"];
        if (postid != null)
        {

            string path = "C:\\Users\\user\\Source\\Repos\\FoodBlog\\FoodBlog\\Data\\blog_post.xml";

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            XDocument xdoc = XDocument.Load(fs);
            var post = from p in xdoc.Descendants("post")
                       where (string)p.Element("pid")== postid
                       select new
                       {
                           Title = p.Element("title"),
                           Subtitle = p.Element("subtitle"),
                           Desc = p.Element("description"),
                           PID = p.Element("pid")


                       };

            foreach (var ePosts in post)
            {
                if(ePosts.PID.Value==postid)
                ePosts.Title.ReplaceWith(newtitle.Text);

                ePosts.Subtitle.ReplaceWith(Subtitle.Value);
                ePosts.Desc.ReplaceWith(update_des.Value);
            }




        }

XML data structure :

<Posts>
  <post>Something<
  <pid>p9119</pid>
  <title>Something</title>
  <description>Something</description>
  <subtitle>Something</subtitle>
  <date>Something</date>
  <author>Something</author>
</Posts>




















    }
}

}




Aucun commentaire:

Enregistrer un commentaire