关于.net:从xml生成逗号分隔的字符串而不使用XSLT | 珊瑚贝

Generate comma delimited string from xml without using XSLT


我有一个如下所列的 XML。我需要创建一个逗号分隔的 AdvancedShipNotice 值字符串。我们如何在不使用 XSLT 的情况下使用 XPath?

注意:目前我正在遍历每个节点 – 但我正在寻找更好的方法

参考

  • XSLT:从元素值生成逗号分隔的字符串
  • XML

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <root>
    <AdvShipNotices>
        <AdvancedShipNotice>6D23513</AdvancedShipNotice>
        <StatusCD>OS</StatusCD>
        <CreatedOnDate>2014-03-28T11:08:16.750</CreatedOnDate>
        <TextilePlantCD>6D </TextilePlantCD>
    </AdvShipNotices>
    <AdvShipNotices>
        <AdvancedShipNotice>6D23514</AdvancedShipNotice>
        <StatusCD>OS</StatusCD>
        <CreatedOnDate>2014-03-28T11:08:16.750</CreatedOnDate>
        <TextilePlantCD>6D </TextilePlantCD>
    </AdvShipNotices>
    </root>

    VB.Net

    1
    2
    3
    4
    Dim objXML As New XmlDocument
    Dim asnString As String
    asnString =“<root>” & objASN.GetAdvShipNotices(containerScanParameter.PlantCD, containerScanParameter.UserID, , ,“OS”) &“</root>”
    objXML.LoadXml(asnString)
    • 有什么尝试吗?所以我们不需要从头开始(遍历 AdvShipNotices 并用 String.Join 和 XmlNode.InnerText 构建一个字符串)…
    • @AdrianoRepetti 目前我正在遍历每个节点-但我正在寻找更好的代码
    • 在 XPath 2.0 中,您可以使用 string-join(/root/AdvShipNotices/AdvancedShipNotice, ‘,’)


    你可以这样做:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    StringReader reader = new StringReader( @
      <doc>
        <e>a</e>
        <e>b</e>
        <e>c</e>
        <e>d</e>
        <e>e</e>
      </doc>”
    .Trim()
      ) ;

    XmlDocument xml = new XmlDocument() ;
    xml.Load( reader ) ;

    IEnumerable<string> texts = xml
                                .SelectNodes(“//*[text()]” )
                                .Cast<XmlNode>()
                                .Select( x => x.InnerText )
                                ;
    string csv = String.Join(“,” , texts ) ;

    最后,csv 应该持有 a,b,c,d,e.

    根据您的 XML 结构,您可能需要调整 XPath 表达式以适应。

    另一种方法使用 XDocument。对于您的示例文档,可以使用以下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    string xml =
    @“<root>
        <AdvShipNotices>
          <AdvancedShipNotice>6D23513</AdvancedShipNotice>
          <StatusCD>OS</StatusCD>
          <CreatedOnDate>2014-03-28T11:08:16.750</CreatedOnDate>
          <TextilePlantCD>6D </TextilePlantCD>
        </AdvShipNotices>
        <AdvShipNotices>
          <AdvancedShipNotice>6D23514</AdvancedShipNotice>
          <StatusCD>OS</StatusCD>
          <CreatedOnDate>2014-03-28T11:08:16.750</CreatedOnDate>
          <TextilePlantCD>6D </TextilePlantCD>
        </AdvShipNotices>
      </root>”
    ;

    XDocument doc     = XDocument.Parse( xml ) ;
    string    csvFile = string.Join( Environment.NewLine ,
                          doc
                          .Root
                          .Elements()
                          .Select( e => string.Join(“,” ,
                              e
                              .Elements()
                              .Select( c => c.Value )
                              )
                            )
                        ) ;

    产生这个文本

    1
    2
    6D23513,OS,2014-03-28T11:08:16.750,6D
    6D23514,OS,2014-03-28T11:08:16.750,6D

    来源:https://www.codenong.com/24415606/

    微信公众号
    手机浏览(小程序)

    Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9003.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
    0
    分享到:
    没有账号? 忘记密码?