18 Haziran 2015 Perşembe

Finding Xslt File Path In XmlDocument Object.

if you have an xml file below.And if you want to extract xslt file path...
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xxxxx.xslt"?>
<xxx:xxxx>data</xxx:xxx>
view raw Test Xslt Xml hosted with ❤ by GitHub
This code snippet detect xslt file path in xml file
using System;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
public static class XmlExtension
{
const int HREF_LENGTH = 6;
public static String FindXsltFilePath(this XmlDocument doc)
{
String result = String.Empty;
XmlProcessingInstruction xmlStylesheet = null;
foreach (XmlNode xmlNode in doc.ChildNodes)
{
if (xmlNode is XmlProcessingInstruction)
{
xmlStylesheet = (XmlProcessingInstruction)xmlNode;
if (xmlStylesheet.Value.Length > 0)
{
string[] valueHref = xmlStylesheet.Value.Split(' ');
if (valueHref.Length > 1) result = FindLinkValue(valueHref[1]);
}
break;
}
}
return result;
}
private static String FindLinkValue(String href)
{
return href.Substring(HREF_LENGTH, href.Length - HREF_LENGTH).Replace("\"", "");
}
}
How to use ?
using System;
using System.Xml;
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load("yourfile.xml");
String xsltFilePath = doc.FindXsltFilePath();
}
}