如何从给定的XSLT文件中获取版本号。

时间:2017-11-11 08:07:23

标签: c# .net xml xslt-1.0 xslt-3.0

假设我有一个类似下面的XSLT文件:

istream

我需要输出为3.0,因为上面的文件有version =“3.0”。鉴于XSLT是字符串格式

,我想使用C#来获取此信息

2 个答案:

答案 0 :(得分: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");
        }
    }
}

答案 1 :(得分: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