Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 724057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:10:44+00:00 2026-05-14T06:10:44+00:00

I’m a LAMP guy and ended up working this small news module for an

  • 0

I’m a LAMP guy and ended up working this small news module for an asp.net site, which I am having some difficulty with. I basically am adding and deleting elements via AJAX based on the id. Before, I had it working based on the the index of a set of elements, but would have issues deleting, since the index would change in the xml file and not on the page (since I am using ajax).

Here is the rundown

news.xml

<?xml version="1.0" encoding="utf-8"?>
<news>
  <article id="1">
    <title>Red Shield Environmental implements the PARCSuite system</title>
    <story>Add stuff here</story>
  </article>
  <article id="2">
    <title>Catalyst Paper selects PARCSuite for its Mill-Wide Process...</title>
    <story>Add stuff here</story>
  </article>
  <article id="3">
    <title>Weyerhaeuser uses Capstone Technology to provide Control...</title>
    <story>Add stuff here</story>
  </article>
</news>

Page sending del request:

<script type="text/javascript">
        $(document).ready(function () {
            $('.del').click(function () {
                var obj = $(this);
                var id = obj.attr('rel');
                $.post('add-news-item.aspx',
                    { id: id },
                    function () {
                        obj.parent().next().remove();
                        obj.parent().remove();
                    }
                );
            });
        });
    </script>
    <a class="del" rel="1">...</a>
    <a class="del" rel="1">...</a>
    <a class="del" rel="1">...</a>

My functions

protected void addEntry(string title, string story)
    {
        XmlDocument news = new XmlDocument();
        news.Load(Server.MapPath("../news.xml"));

        XmlAttributeCollection ids = news.Attributes;
        //Create a new node
        XmlElement newelement = news.CreateElement("article");
        XmlElement xmlTitle = news.CreateElement("title");
        XmlElement xmlStory = news.CreateElement("story");

        XmlAttribute id = ids[0];
        int myId = int.Parse(id.Value + 1);
        id.Value = ""+myId;
        newelement.SetAttributeNode(id);
        xmlTitle.InnerText = this.TitleBox.Text.Trim();
        xmlStory.InnerText = this.StoryBox.Text.Trim();


        newelement.AppendChild(xmlTitle);
        newelement.AppendChild(xmlStory);

        news.DocumentElement.AppendChild(newelement);

        news.Save(Server.MapPath("../news.xml"));
    }

    protected void deleteEntry(int selectIndex)
    {
        XmlDocument news = new XmlDocument();
        news.Load(Server.MapPath("../news.xml"));

        XmlNode xmlnode = news.DocumentElement.ChildNodes.Item(selectIndex);
        xmlnode.ParentNode.RemoveChild(xmlnode);
        news.Save(Server.MapPath("../news.xml"));
    }

I haven’t updated deleteEntry() and you can see, I was using the array index but need to delete the article element based on the article id being passed. And when adding an entry, I need to set the id to the last elements id + 1. Yes, I know SQL would be 100 times easier, but I don’t have access so… help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:10:44+00:00Added an answer on May 14, 2026 at 6:10 am

    Linq to XML should make this a lot simpler. Here would be the equivalent of what you are trying:

            public void AddEntry(string title, string story)
            {
                var newElement = new XElement("article", new XElement("title", title), new XElement("story", story));                
                XDocument doc = XDocument.Parse(testXml);
                var maxId = doc.Descendants("article").Attributes("id").Max(x => int.Parse(x.Value));
                newElement.Add(new XAttribute("id", ++maxId));
                doc.Descendants("news").First().Add(newElement);
                //save the document
            }
    
            public void DeleteEntry(int selectIndex)
            {
                XDocument doc = XDocument.Parse(testXml);
                doc.Descendants("article").Where(x => int.Parse(x.Attribute("id").Value) == selectIndex).Remove();
                //save the document
            }
    

    Based on the size of your xml file and the number of requests, you may want to look into other approaches than loading the document, and saving it for each call to add and delete.

    EDIT: Note that you would need to add null checks to the above code…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.