合并时XPath表达式不起作用

时间:2019-03-07 13:20:13

标签: c# csv xpath expression

在结合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>

am尝试获取特定的标签名称值,例如

  • d:Guid
  • d:ProcessType
  • 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();

但是当我使用合并时,它并不会返回我所需的值,尽管我使用了在线工具来验证我的xPath表达式。

这是我使用合并时的表达方式:

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

有人可以告诉我为什么吗?正确使用我的表情需要做什么?

2 个答案:

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

答案 1 :(得分:0)

在这里,您可以选择使用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")
        .WithXmlNamespaceManager(nsManager)
        .WithField("Guid", xPath: "d:Guid")
        .WithField("ProcessType", xPath: "d:ProcessType")
        .WithField("Description", xPath: "d:Description")
    )
{
    using (var w = new ChoCSVWriter(csv)
        .WithFirstLineHeader()
        )
        w.Write(p);
}

Console.WriteLine(csv);