Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.Net (C#) - Adding to and transforming an XML file

Status
Not open for further replies.

URLJones

Programmer
Jun 14, 2004
42
CA
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:

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
 
Nevermind. I solved the problem. There was something wrong with the XSL stylesheet. I was trying to access the three nodes inside the stylesheet.

BEFORE:
Code:
<xsl:value-of select="CurrentLanguage"/>
<xsl:value-of select="Key"/>
<xsl:value-of select="Val"/>

AFTER:
Code:
<xsl:value-of select="CurrentLanguage"/>
<xsl:value-of select="Root/Key"/>
<xsl:value-of select="Root/Val"/>

I'm not sure why the first node (the CurrentLanguage node) wasn't also included under the Root node (like the other two elements), though.

If someone wants to answer THAT question, be my guest.

Thanks,

Ryan
 
Can you better explain what you're trying to accomplish? Are you getting any error message?

Also, out of curiosity, what drove the decision to use XSLT?

.NET has some powerful, easy to work with mechanisms for doing things like displaying data formatted as HTML (through server controls like GridViews) and displaying data for different cultures (through System.Globalization and related classes).

It could be that you'd save a lot of work and gain a lot of power by considering a different methodology (depending on your circumstances).

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top