Hi. I have a question with regard to Adding/Transforming an XML file within an .aspx file using C#. Basically, I can read in my XML file using C#, but I would like to add some nodes to it (one for language, one for a key, and one for a value). Then I would like to have the xml file transform using a supplied xsl stylesheet. Here is the code I have implemented so far:
If anyone can provide some assistance, it would be very much appreciated.
Thanks,
URL
Code:
void Page_Load() {
string lang = Request.QueryString["lang"];
string xmlFile = Request.QueryString["xmlFile"];
string key = Request.QueryString["key"];
string val = Request.QueryString["val"];
try{
//load the xmlFile.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(xmlFile));
//create a node to append to the xmlFile.
XmlNode node;
node = xmlDoc.CreateNode(XmlNodeType.Element, "CurrentLanguage", "");
node.InnerText = lang;
//append the language node.
XmlNode root = xmlDoc.DocumentElement;
root.AppendChild(node);
//create a node to append the elementID to the node.
node = xmlDoc.CreateNode(XmlNodeType.Element, "Key", "");
node.InnerText = key;
//append the language node.
root.AppendChild(node);
//create a node to append the elementID to the node.
node = xmlDoc.CreateNode(XmlNodeType.Element, "Val", "");
node.InnerText = val;
//append the language node.
root.AppendChild(node);
XPathNavigator nav = xmlDoc.CreateNavigator();
XslTransform tr = new XslTransform();
tr.Load(Server.MapPath("Create_Table.xsl"));
StringWriter sWriter = new StringWriter();
XmlTextWriter myWriter = new XmlTextWriter(sWriter);
tr.Transform(xmlDoc, null, myWriter);
myWriter.Flush();
Response.Write(sWriter.ToString());
}
If anyone can provide some assistance, it would be very much appreciated.
Thanks,
URL