Reputation: 489
Is there a way for me to change the stylesheet tag in an xml. Is there a way to do that.... ex if I receive
<?xml version="1.0" encoding="us-ascii"?>
<?xml-stylesheet type="text/xsl" href="www.somecompany.com/stylesheet.xsl"?>
<MedicalRecord>
......
......
</MedicalRecord>
I want to make it
<?xml version="1.0" encoding="us-ascii"?>
<?xml-stylesheet type="text/xsl" href="mystylesheet.xsl"?>
<MedicalRecord>
......
......
</MedicalRecord>
Note the href tag value has changed.
Upvotes: 2
Views: 407
Reputation: 119816
At it's simplest based on a straight replace:
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
XmlProcessingInstruction pi =
(XmlProcessingInstruction)
doc.SelectSingleNode("/processing-instruction('xml-stylesheet')");
// Replace href with the one we want
Regex r = new Regex("href=[\"']?((?:.(?![\"']?\\s+(?:\\S+)=|[>\"']))+.)[\"']?",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
pi.Data = r.Replace(pi.Data, "href=\"mystyle.xsl\"", 1, 0);
Upvotes: 2