2017-11-11 144 views
2

說我有一個XSLT文件象下面這樣:如何從給定的XSLT文件中獲取版本號。

`<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result- 
     prefixes="xs math" 
     version="3.0">` 

....... and so on. 

我所需要的輸出作爲3.0由於上述文件具有版本=「3.0」。我想使用C#來獲得這個給定的XSLT是字符串格式

回答

1

使用XElement.Parse(yourString).Attribute("version").Value,其中您添加using System.Xml.Linq;。有關使用的API的詳細信息,請參閱https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview

+0

如果輸入是一個流而不是字符串? – swifty

+0

那麼,如果你想像任何其他API那樣使用這個API,那麼就開始閱讀它的文檔,類'XElement'有一個'Load'方法https://docs.microsoft.com/en-us/dotnet/api/system .xml.linq.xelement.load?view = netframework-4.7.1,它有一個負載以'Stream'爲例。 –

2

使用XML LINQ:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      string version = (string)doc.Root.Attribute("version"); 
     } 
    } 
}