2

假设我有一个 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,因为上面的文件有 version="3.0"。鉴于 XSLT 是字符串格式,我想使用 C# 来获得它

4

2 回答 2

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");
        }
    }
}
于 2017-11-11T09:06:12.600 回答
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

于 2017-11-11T09:00:43.857 回答