xml and .net

public void AddChild(XmlDocument doc, XmlNode parentNode, string elementName, string value)
{
    XmlNode node = doc.CreateElement(elementName);
    node.InnerText = value;
    parentNode.AppendChild(node);
}

public bool Save()
{
    Buiz.Generic generic = new Buiz.Generic();
    XmlDocument doc = new XmlDocument();
    doc.Load(fileLocation);
    XmlNode node = doc.CreateNode(XmlNodeType.Element,fileMasterNode, null);
    generic.AddChild(doc, node, "ID", "ABC");
    #region Add to Parent Element Node & save
        //add to elements collection
    doc.DocumentElement.AppendChild(node);
    doc.Save(fileLocation);
    #endregion             
}

Open in new window


<Quotes>
  <Quote>
    <ID>61c6a1d9-bb7a-4525-880c-81e7eaae284e</ID>
  </Quote>
</Quotes>

Open in new window


Now
I want to result in XML like below(Basically just adding another level inside of xml):

<Quotes>
  <Quote>
    <GenericInfo>
    <ID>61c6a1d9-bb7a-4525-880c-81e7eaae284e</ID>
    </GenericInfo>
  </Quote>
</Quotes>

Open in new window


How can I do that within my codes?

Thanks
LVL 1
ITsolutionWizardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YZlatCommented:
try this:

XmlDocument doc = new XmlDocument();
            doc.Load(@"your path here");
            XmlNode childNode = doc.SelectSingleNode("/Quotes/Quote/ID"); 
            XmlNode pnode = childNode.ParentNode;
            childNode.ParentNode.RemoveChild(childNode);
            XmlNode new_node = doc.CreateElement("GenericInfo");
            new_node.AppendChild(childNode);
            pnode.AppendChild(new_node);
            doc.Save(@"your path here");

Open in new window

0
ITsolutionWizardAuthor Commented:
what if below:

<Quotes>
  <Quote>
    <GenericInfo>
    <ID>61c6a1d9-bb7a-4525-880c-81e7eaae284e</ID>
    </GenericInfo>
    <OtherInfo>
     <ID>32434A</ID>
    </OtherInfo>
  </Quote>
</Quotes>

I will have more than one
0
ITsolutionWizardAuthor Commented:
And please try to use my codes to modify.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

YZlatCommented:
That was not the original question. Also you did not post the complete code. Where do you call your Save method? Where is class generic defined? Does AddChild belongs to that class?  Otherwise I did give you a solution to your question. It words and you can easily change the code to fit in your class
0
YZlatCommented:
 public void AddChild(XmlDocument doc, XmlNode parentNode, string elementName, string value)
        {
            XmlNode node = doc.CreateElement(elementName);
            node.InnerText = value;
            parentNode.AppendChild(node);

        }

        public  bool Save(string fileLocation)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(fileLocation);
                XmlNode childNode = doc.SelectSingleNode("/Quotes/Quote/ID"); // apply your xpath here
                XmlNode pnode = childNode.ParentNode;
                childNode.ParentNode.RemoveChild(childNode);
                XmlNode new_node = doc.CreateElement("GenericInfo");
                new_node.AppendChild(childNode);
                pnode.AppendChild(new_node);
                XmlNode other_node = doc.CreateElement("OtherInfo");
                AddChild(doc, pnode.AppendChild(other_node), "ID", "ABC");
                doc.Save(fileLocation);
                return true;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }

        }

Open in new window

0
ITsolutionWizardAuthor Commented:
YZlat: Your codes is ended up return False. it is not working
0
YZlatCommented:
I tested it and it worked for me. the problem is that you did not post all your code. If you do, I can help you fix it
0
ITsolutionWizardAuthor Commented:
As I told you. it is not working

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace Web01
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            Response.Write(Save2(@"I:\My Hard Drive\Projects\InsurSol\Data\Quote.xml"));
     
        }

        public void AddChild(XmlDocument doc, XmlNode parentNode, string elementName, string value)
        {
            XmlNode node = doc.CreateElement(elementName);
            node.InnerText = value;
            parentNode.AppendChild(node);

        }

        public bool Save2(string fileLocation)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(fileLocation);
                XmlNode childNode = doc.SelectSingleNode("/Quotes/Quote"); // apply your xpath here
                XmlNode pnode = childNode.ParentNode;
                childNode.ParentNode.RemoveChild(childNode);
                XmlNode new_node = doc.CreateElement("GenericInfo");
                new_node.AppendChild(childNode);
                pnode.AppendChild(new_node);
                XmlNode other_node = doc.CreateElement("OtherInfo");
                AddChild(doc, pnode.AppendChild(other_node), "ID", "ABC");
                doc.Save(fileLocation);
                return true;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }

        }

    }
}
0
YZlatCommented:
OK, now I see what the problem is - you are using a web application. I did it with windows app. The code is correct and if you run your web application from your computer, it works. It worked for me when I copied the code you used. The problem is that when application is on web server and the XML file you are trying to modify is not local to application (is not on the same physical server), it will not work. That's just how it is for security reason - so that web apps cannot modify files on your hard drive
0
ITsolutionWizardAuthor Commented:
An entire codes are in my computer. not a server. and it returns false. everything include the xml is in  the same directory. there is no security reason.
0
YZlatCommented:
Well, the code worked for me just fine in both windows and web apps
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YZlatCommented:
Can you give me copy of your file? Perhaps the key to the problem is the file itself
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.