Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

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
Avatar of YZlat
YZlat
Flag of United States of America image

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

Avatar of ITsolutionWizard

ASKER

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
And please try to use my codes to modify.
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
 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

YZlat: Your codes is ended up return False. it is not working
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
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;
            }

        }

    }
}
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
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.
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you give me copy of your file? Perhaps the key to the problem is the file itself