I have a xml file and an xslt file in my website project.
xml file:
Kaushal
You could consider using the XmlDocument class here;
For example, you actually have the XML you want in the stream output from your XslTransform:
XmlDocument resultDocument = new XmlDocument();
resultDocument.Load(objStream);
resultDocument.Save(Server.MapPath("XMLFile.xml"));
EDIT. You'll also need to change your XSLT for this to work. Something like:
Mr.
24
Should work. However, this is probably not the best approach as you need to supply an age per person and you don't seem to be sourcing this from anywhere. I'd recommend the DOM approach.
You also have to be careful here, though; it's conceivable that there might be a read lock on the file if it's being read by another process. If this is a one off exercise I'd consider just doing this prior to deploying your website.
Another alternative is to not use XSLT. In this instance you could also consider simple DOM processing:
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("XMLFile.xml"));
XmlNodeList employeeNodes = doc.SelectNodes("//employee");
foreach(XmlNode employeeNode in employeeNodes)
{
employeeNode.SelectSingleNode("./firstname").InnerText = String.Format("Mr. {0}", employeeNode.SelectSingleNode("./firstname").InnerText);
XmlNode ageNode = doc.CreateElement("age");
ageNode.InnerText = "3 Billion Years";
employeeNode.ChildNodes.Add(ageNode0;
}
doc.Save();
I haven't compile checked this code but hopefully the intent is clear. Note that you could also use XDocument if you preferred.