简体   繁体   English

不使用XSLT从xml生成逗号分隔的字符串

[英]Generate comma delimited string from xml without using XSLT

I have a XML as listed below. 我有一个XML,如下所示。 I need to create a comma separated string of AdvancedShipNotice values. 我需要创建一个逗号分隔的AdvancedShipNotice值字符串。 How do we do it using XPath without using XSLT? 在不使用XSLT的情况下如何使用XPath做到这一点?

Note: Currently I am looping throgh each node - but I am looking for a better approach 注意:目前,我正在遍历每个节点-但我正在寻找更好的方法

Reference 参考

  1. XSLT: Generate comma-separated string from element values XSLT:从元素值生成逗号分隔的字符串

XML 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>

VB.Net VB.Net

Dim objXML As New XmlDocument
Dim asnString As String
asnString = "<root>" & objASN.GetAdvShipNotices(containerScanParameter.PlantCD, containerScanParameter.UserID, , , "OS") & "</root>"
objXML.LoadXml(asnString)

You can do something like this: 您可以执行以下操作:

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 ) ;

At the end of which, csv should hold a,b,c,d,e . 最后, csv应该a,b,c,d,e

Depending on the structure of your XML, you might have to adjust the XPath exression to suit. 根据XML的结构,您可能必须调整XPath的扩展名才能适应。

Another approach uses XDocument . 另一种方法使用XDocument For your sample document, something like this will work: 对于您的示例文档,类似这样的方法将起作用:

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 )
                          )
                        )
                    ) ;

producing this text 产生这段文字

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM