【问题标题】:XDocument to get the attribute of a preprocessing instructionXDocument 获取预处理指令的属性
【发布时间】:2013-08-03 05:15:55
【问题描述】:

我想访问一个预处理指令“mso-infoPathSolution”,它有一个属性 href 并为其分配一个 url,比如http://www.msn.com

<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:XMLtool:-myXSD-2013-06-22T17-00-48" solutionVersion="1.0.0.457" productVersion="14.0.0.0" PIVersion="1.0.0.0"  href="" ?>

我需要使用 XDocument 类。我如何做到这一点?

【问题讨论】:

    标签: xml linq


    【解决方案1】:

    使用 VB.NET,我能够确定处理指令被编码为 XProcessingInstruction,它被简单地分解为

    • Target(在您的示例中为mso-infoPathSolution),以及
    • Dataname="value" 对的其余部分,未解析),
    • Strings.

    所以你需要检查一个现有的href="...",如果存在则调整它,否则添加它:

    Dim xpi = xDoc.Nodes.OfType(Of XProcessingInstruction) _
              .Where(Function(p) p.Target = "mso-infoPathSolution").First
    Dim re = New Regex("href=""([^""]*)""")
    If re.IsMatch(xpi.Data) Then
      xpi.Data = re.Replace(xpi.Data, "href=""" & Url & """")
    Else
      xpi.Data &= " href=""" & Url & """"
    End If
    

    【讨论】:

      【解决方案2】:

      在 C# 中:
      1. 访问 XDocument
      的 XProcessingInstruction(s) 2.看下面的例子如何访问和更改处理字符串:
      MSDNlink

      (阅读全文或滚动到“示例”)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-23
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-15
        相关资源
        最近更新 更多