ITsolutionWizard
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
}
<Quotes>
<Quote>
<ID>61c6a1d9-bb7a-4525-880c-81e7eaae284e</ID>
</Quote>
</Quotes>
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>
How can I do that within my codes?
Thanks
ASKER
what if below:
<Quotes>
<Quote>
<GenericInfo>
<ID>61c6a1d9-bb7a-4525-880 c-81e7eaae 284e</ID>
</GenericInfo>
<OtherInfo>
<ID>32434A</ID>
</OtherInfo>
</Quote>
</Quotes>
I will have more than one
<Quotes>
<Quote>
<GenericInfo>
<ID>61c6a1d9-bb7a-4525-880
</GenericInfo>
<OtherInfo>
<ID>32434A</ID>
</OtherInfo>
</Quote>
</Quotes>
I will have more than one
ASKER
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;
}
}
ASKER
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
ASKER
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\Da ta\Quote.x ml"));
}
public void AddChild(XmlDocument doc, XmlNode parentNode, string elementName, string value)
{
XmlNode node = doc.CreateElement(elementN ame);
node.InnerText = value;
parentNode.AppendChild(nod e);
}
public bool Save2(string fileLocation)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(fileLocation);
XmlNode childNode = doc.SelectSingleNode("/Quo tes/Quote" ); // apply your xpath here
XmlNode pnode = childNode.ParentNode;
childNode.ParentNode.Remov eChild(chi ldNode);
XmlNode new_node = doc.CreateElement("Generic Info");
new_node.AppendChild(child Node);
pnode.AppendChild(new_node );
XmlNode other_node = doc.CreateElement("OtherIn fo");
AddChild(doc, pnode.AppendChild(other_no de), "ID", "ABC");
doc.Save(fileLocation);
return true;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return false;
}
}
}
}
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:\
}
public void AddChild(XmlDocument doc, XmlNode parentNode, string elementName, string value)
{
XmlNode node = doc.CreateElement(elementN
node.InnerText = value;
parentNode.AppendChild(nod
}
public bool Save2(string fileLocation)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(fileLocation);
XmlNode childNode = doc.SelectSingleNode("/Quo
XmlNode pnode = childNode.ParentNode;
childNode.ParentNode.Remov
XmlNode new_node = doc.CreateElement("Generic
new_node.AppendChild(child
pnode.AppendChild(new_node
XmlNode other_node = doc.CreateElement("OtherIn
AddChild(doc, pnode.AppendChild(other_no
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
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you give me copy of your file? Perhaps the key to the problem is the file itself
Open in new window