XPath函数在net framewok中不起作用,但适用于SQL查询

时间:2017-08-15 22:05:32

标签: c# .net tsql xpath

我有以下XML和在SQL Server 2008中运行良好的查询,但是当我在C#中使用Concat对象选择具有函数SubstringXmlDocument的相同节点时失败。

可能是什么问题?

declare @x xml='<asn>
      <asnH>
        <senderId>9053357373T</senderId>
        <pono>SCA0055792</pono>
        <dino>440229</dino>
        <shipmentWeight>1109</shipmentWeight>
        <loadingQty>190</loadingQty>di
        <zWeight>1109</zWeight>
      </asnH>
      <dd>
        <COMNO>010</COMNO>
        <T_x0024_ORNO>910055</T_x0024_ORNO>
        <T_x0024_CFRW>UP1</T_x0024_CFRW>
        <T_x0024_TRNO>abc-12345                     </T_x0024_TRNO>
        <T_x0024_DINO>440229</T_x0024_DINO>
        <T_x0024_ITEM>      817025-001</T_x0024_ITEM>
        <T_x0024_PONO>1</T_x0024_PONO>
        <T_x0024_DDAT>2017-08-14T00:00:00-04:00</T_x0024_DDAT>
        <T_x0024_DQUA>10</T_x0024_DQUA>
        <T_x0024_OQUA>10</T_x0024_OQUA>
        <T_x0024_WGHT>15</T_x0024_WGHT>
        <T_x0024_CUPS>ea </T_x0024_CUPS>
        <T_x0024_TRID>66085</T_x0024_TRID>
        <lWeight>150</lWeight>
      </dd>
    </asn>'

    select 
        t.c.query('concat(substring(asn[1]/asnH[1]/senderId[1],1,5),asn[1]/asnH[1]/dino[1])') 
    from
        @x.nodes('.')  t(c)

1 个答案:

答案 0 :(得分:1)

这给我的结果:90533440229

string xml = @"<asn>
    <asnH>
    <senderId>9053357373T</senderId>
    <pono>SCA0055792</pono>
    <dino>440229</dino>
    <shipmentWeight>1109</shipmentWeight>
    <loadingQty>190</loadingQty>di
    <zWeight>1109</zWeight>
    </asnH>
    <dd>
    <COMNO>010</COMNO>
    <T_x0024_ORNO>910055</T_x0024_ORNO>
    <T_x0024_CFRW>UP1</T_x0024_CFRW>
    <T_x0024_TRNO>abc-12345                     </T_x0024_TRNO>
    <T_x0024_DINO>440229</T_x0024_DINO>
    <T_x0024_ITEM>      817025-001</T_x0024_ITEM>
    <T_x0024_PONO>1</T_x0024_PONO>
    <T_x0024_DDAT>2017-08-14T00:00:00-04:00</T_x0024_DDAT>
    <T_x0024_DQUA>10</T_x0024_DQUA>
    <T_x0024_OQUA>10</T_x0024_OQUA>
    <T_x0024_WGHT>15</T_x0024_WGHT>
    <T_x0024_CUPS>ea </T_x0024_CUPS>
    <T_x0024_TRID>66085</T_x0024_TRID>
    <lWeight>150</lWeight>
    </dd>
</asn>";

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.InnerXml = xml;
XmlNode root = xmlDocument.DocumentElement;
string xpath = "concat(substring(asn[1]/asnH[1]/senderId[1],1,5),asn[1]/asnH[1]/dino[1])";
string s = root.ParentNode.CreateNavigator().Evaluate(xpath).ToString();
Console.WriteLine(s);