Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将重复组转换为多个文本行_C#_Xslt_Transformation_Repeat - Fatal编程技术网

C# 将重复组转换为多个文本行

C# 将重复组转换为多个文本行,c#,xslt,transformation,repeat,C#,Xslt,Transformation,Repeat,我想将重复组从示例XML转换为两个分隔的文本行。以下是我当前的代码版本: string orderXml = @"<?xml version='1.0' encoding='utf-8'?> <Order id='79223510'> <Status>new</Status> <ShipMethod>Standard International</ShipMethod> <ToCity>Tokyo</To

我想将重复组从示例XML转换为两个分隔的文本行。以下是我当前的代码版本:

string orderXml = 
@"<?xml version='1.0' encoding='utf-8'?>
<Order id='79223510'>
<Status>new</Status>
<ShipMethod>Standard International</ShipMethod>
<ToCity>Tokyo</ToCity>
<Items>
    <Item>
    <SKU>SKU-1234567890</SKU>
    <Quantity>1</Quantity>
    <Price>99.95</Price>
    </Item>
    <Item>
    <SKU>SKU-1234567899</SKU>
    <Quantity>1</Quantity>
    <Price>199.95</Price>
    </Item>
</Items>
</Order>";

StringReader str = new StringReader(orderXml);

var xslt = new XmlTextReader(new StringReader(
@"<?xml version='1.0' encoding='UTF-8'?>" +
"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +

"<xsl:output method='text' indent='no'/>" +

"<!-- New line character variable -->" +
"<xsl:variable name='newline'>" +
"<xsl:text>&#13;&#10;</xsl:text>" +
"</xsl:variable>" +
"<xsl:variable name='empty'>" +
"<xsl:text></xsl:text>" +
"</xsl:variable>" +

"<xsl:template match='/'>" +         

"<xsl:for-each select='Order'>" +
"<xsl:value-of select='@id'/>|" + 
"<xsl:value-of select='Status'/>|" +
"<xsl:value-of select='ShipMetod'/>|" +
"<xsl:value-of select='ToCity'/>|" + 
"<xsl:value-of select='$newline'/>" +
"</xsl:for-each>" +

"<xsl:for-each select='Order/Items/Item'>" +
"<xsl:value-of select='SKU'/>|" +          
"<xsl:value-of select='Quantity'/>|" +   
"<xsl:value-of select='Price'/>|" +    
"<xsl:value-of select='$newline'/>" +
"</xsl:for-each>" + 

"</xsl:template>" +
"</xsl:stylesheet>"
                ));

var xDoc = new XPathDocument(str);
var xTr = new System.Xml.Xsl.XslCompiledTransform();
xTr.Load(xslt);

StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
xTr.Transform(xDoc, null, writer);

string[] lines = sb.ToString().Split(new string[] {"\n"}, StringSplitOptions.RemoveEmptyEntries);    

lines.ToList().ForEach(System.Console.Write);     
我希望它能生产出以下产品:

79223510|new||Tokyo|SKU-1234567890|1|99.95|
79223510|new||Tokyo|SKU-1234567899|1|199.95|
只需使用XSL转换。请告知

另外,为了便于阅读,以文本形式添加了XSL:

@<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method='text' indent='no'/>

<!-- New line character variable -->
<xsl:variable name='newline'>
<xsl:text>&#13;&#10;</xsl:text>
</xsl:variable>
<xsl:variable name='empty'>
<xsl:text></xsl:text>
</xsl:variable>

<xsl:template match='/'>         

<xsl:for-each select='Order'>
<xsl:value-of select='@id'/>| 
<xsl:value-of select='Status'/>|
<xsl:value-of select='ShipMetod'/>|
<xsl:value-of select='ToCity'/>| 
<xsl:value-of select='$newline'/>
</xsl:for-each>

<xsl:for-each select='Order/Items/Item'>
<xsl:value-of select='SKU'/>|          
<xsl:value-of select='Quantity'/>|   
<xsl:value-of select='Price'/>|    
<xsl:value-of select='$newline'/>
</xsl:for-each> 

</xsl:template>
</xsl:stylesheet>
@


| 
|
|
| 
|          
|   
|    

您犯了一些错误:

  • 每个循环使用两个单独的for-each循环,而不是循环中的一个循环,因此第一行输出来自
    Order
    元素,接下来的两行来自
    Item
    元素。(实际上,您不需要任何循环。解决方案如下。)
  • 您拼错了
    ShipMetod
    ,因此其中一个字段错误地为空
  • 您没有任何方法可以转义字段值中可能存在的任何
    |
    值。(我的解决方案无法解决此问题,因为我不确定您要对此做什么。)
  • 更好的解决方案是使用模板匹配和字符串连接

    
    
    
    |
    
    这将产生以下输出:

    79223510|new||Tokyo|
    SKU-1234567890|1|99.95|
    SKU-1234567899|1|199.95|
    
    79223510|new|Standard International|Tokyo|SKU-1234567890|1|99.95|
    79223510|new|Standard International|Tokyo|SKU-1234567890|1|199.95|