【问题标题】:XPath expression does not work while combiningXPath 表达式在组合时不起作用
【发布时间】:2019-07-29 09:36:20
【问题描述】:

在 xpath 表达式中组合时遇到问题:

这是我的 xml:

<m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<d:Guid>fizeofnpj-dzeifjzenf-ezfizef</d:Guid>
<d:ObjectId>6000009251</d:ObjectId>
<d:ProcessType>ZMIN</d:ProcessType>
<d:ProcessTypeTxt>Incident</d:ProcessTypeTxt>
<d:Description>Test 2</d:Description>
<d:IntroText>Incident (IT Service Management)</d:IntroText>
<d:CreatedAtDateFormatted>08.05.18</d:CreatedAtDateFormatted>
<d:ChangedAtDateFormatted>08.05.18</d:ChangedAtDateFormatted>
<d:PostingDate>2018-05-08T00:00:00</d:PostingDate>
<d:ChangedAtDate>2018-05-08T00:00:00</d:ChangedAtDate>
<d:Priority>2</d:Priority>
<d:PriorityTxt>2: High</d:PriorityTxt>
<d:PriorityState>None</d:PriorityState>
<d:Concatstatuser>New</d:Concatstatuser>
<d:ActionRequired>false</d:ActionRequired>
<d:StillOpen>true</d:StillOpen>
<d:Icon />
<d:SoldToPartyName />
<d:ServiceTeamName />
<d:PersonRespName />
<d:CategoryTxt>Change - Interface - Evolutive Maintenance</d:CategoryTxt>
<d:ConfigItemTxt />
<d:SAPComponent>BC-BCS-VAS</d:SAPComponent>

我正在尝试获取特定的标签名称值,例如

  • d:Guid
  • d:进程类型
  • d:描述

我的问题是,在我的 c# 代码中这是有效的:

 string xml = System.IO.File.ReadAllText(startupPath);

        StringBuilder sb = new StringBuilder();

        var nsManager = new XmlNamespaceManager(new NameTable());
        //register mapping of prefix to namespace uri 
        nsManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
        nsManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");




        using (var node = ChoXmlReader.LoadText(xml)
          .WithXPath("//entry/content/m:properties/d:Guid")
          .WithXmlNamespaceManager(nsManager)
          )

        {
            using (var w = new ChoCSVWriter(sb).WithFirstLineHeader())
            {
                w.Write(node);
            }
        }


        StreamWriter sw = new StreamWriter(resultsPath);

        Console.WriteLine("csv" + sb.ToString());
        sw.WriteLine(sb.ToString());

        sw.Close();

但是当我使用 combine 时,它​​并没有返回我需要的值,尽管我使用了在线工具来验证我的 xPath 表达式。

这是我使用 combine 时的表达式:

//content/m:properties/d:Guid | //content/m:properties/d:ObjectId

谁能告诉我为什么会这样?以及需要做什么才能正确使用我的表达方式?

【问题讨论】:

  • /m:properties/d:Guid | /m:properties/d:ObjectId

标签: c# csv xpath expression


【解决方案1】:

在我看来,Cinchoo 的 home grown XPath parser 没有实现对这种语法的任何支持。

你想要的只是使用直接的 LINQ to XML 来实现非常简单,但是:

var doc = XDocument.Load(startupPath);

XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"

var elements = doc.Descendants()
    .Where(e => e.Name == d + "Guid" || e.Name == d + "ObjectId");

foreach (var element in elements)
{
    // write to file
}

【讨论】:

  • 我很震惊;声称这是 XPath 是严重的失实陈述。据我所知,他们的 API 文档没有暗示他们的“XPath”只是一个小子集。
【解决方案2】:

这里是您如何选择选择性节点并使用 Cinchoo ETL 将它们写入 CSV 文件

var nsManager = new XmlNamespaceManager(new NameTable());
//register mapping of prefix to namespace uri 
nsManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
nsManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");

StringBuilder csv = new StringBuilder();
using (var p = ChoXmlReader.LoadText(xml)
        .WithXPath("//entry/content/m:properties", true)
        .WithXmlNamespaceManager(nsManager)
        .WithField("Guid", xPath: "d:Guid")
        .WithField("ProcessType", xPath: "d:ProcessType")
        .WithField("Description", xPath: "d:Description")
    )
{
    using (var w = new ChoCSVWriter(csv)
        .WithFirstLineHeader()
        .Configure(c => c.UseNestedKeyFormat = false)
        )
        w.Write(p);
}

Console.WriteLine(csv);

小提琴样本:https://dotnetfiddle.net/pHzDyE

【讨论】:

  • 非常感谢!但你没有得到文本寿。要检索文本,您应该使用此 xpath using (var p = ChoXmlReader.LoadText(xmlDocument) .WithXPath(Utils.GetXpath()) .WithField("Guid", xPath: "d:Guid/text()") .WithField("ProcessType", xPath: "d:ProcessType/text()") .WithField("Description", xPath: "d:Description/text()") )
猜你喜欢
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多