【问题标题】:How to get the version number from a given XSLT file .如何从给定的 XSLT 文件中获取版本号。
【发布时间】:2018-04-24 10:50:08
【问题描述】:

假设我有一个如下所示的 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# 来获得它

【问题讨论】:

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


    【解决方案1】:

    使用 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");
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用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。

      【讨论】:

      • 如果输入是流而不是字符串怎么办?
      • 好吧,如果你想像使用任何其他 API 一样使用该 API,然后开始阅读它的文档,XElement 类有一个 Load 方法 docs.microsoft.com/en-us/dotnet/api/… 有一个重载采用 Stream例如。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      相关资源
      最近更新 更多