繁体   English   中英

c# XML转换

[英]c# XML transformation

我需要以下 xml 的帮助。 我想知道如何将XML带到下面,我已经尝试了很长时间,但是没有成功。 如果您能对此提供帮助,我将不胜感激。

我可以在php中做同样的事情,但是由于我的数据有点大,php需要很多时间,我用c#从web服务中读取文件并将其保存为xml,但它没有像我一样成功通缉。

 <ResultOfProductList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ServerCode>kenan</ServerCode> <ClientCode>B124565</ClientCode> <Username>karanfil</Username> <ClientIPAddress>78.135.235.3</ClientIPAddress> <Successful>true</Successful> <RequestDate>2022-07-22T12:28:54.6238843+03:00</RequestDate> <ElapsedTimeMS>17</ElapsedTimeMS> <MethodType>GetAllProductsByParts</MethodType> <Result EndOfProducts="false"> <CustomerCode>B124565</CustomerCode> <CustomerName>kenan</CustomerName> <Date>2022-07-22T12:28:54.6238843+03:00</Date> <Brands> <Brand ID="174" Lang="tr" BrandName="PARTSMALL-KORE" StandardName=""/> </Brands> <Products> <Product ID="134898" BrandID="174" ProductCode="KR-PML-PTA-086" ProducerCode="" MinOrderAmount="1" PiecesInBox="1" Unit="PCE" New="false"> <ProductNames> <ProductName Lang="tr">VITES HALATI ( HYUNDAI : ACCENT 95-00 )</ProductName> </ProductNames> <BaseOeNr>43794-22000</BaseOeNr> <Pricing> <ListPriceCurrency>USD</ListPriceCurrency> <LocalCurrency>TLY</LocalCurrency> <CurrencyRate>17.6599</CurrencyRate> <ListPriceWoVAT>31.24</ListPriceWoVAT> <LocalListPriceWVat>651.0004</LocalListPriceWVat> <LocalListPriceWoVat>551.695251</LocalListPriceWoVat> <LocalNetPriceWVat>377.580261</LocalNetPriceWVat> <LocalNetPriceWoVat>319.983246</LocalNetPriceWoVat> <Discount1>42</Discount1> <Discount2>0</Discount2> <Discount3>0</Discount3> <Discount4>0</Discount4> <Discount5>0</Discount5> <Discount6>0</Discount6> <InDiscount>false</InDiscount> </Pricing> <Stocks> <Stock WarehouseID="1" Equality="Eq">0</Stock> <Stock WarehouseID="7" Equality="Eq">0</Stock> <Stock WarehouseID="4" Equality="Eq">0</Stock> <Stock WarehouseID="2" Equality="Eq">0</Stock> <Stock WarehouseID="5" Equality="Eq">0</Stock> </Stocks> </Product> </Result> </ResultOfProductList>

期望的输出

 <Products> <Product> <ID>134898</ID> <BrandID>174</BrandID> <BaseOeNr>43794-22000</BaseOeNr> <ProductCode>KR-PML-PTA-086</ProductCode> <ProductName>VITES HALATI ( HYUNDAI : ACCENT 95-00 )</ProductName> <LocalCurrency>TLY</LocalCurrency> <LocalNetPriceWVat>377.580261</LocalNetPriceWVat> <Stocks>WarehouseID=1 + WarehouseID=7 + WarehouseID=4 + WarehouseID=2 + WarehouseID=5 </Stocks> </Product> </Products>

在此处输入图像描述

这是基于 XSLT 的解决方案。

只是不清楚<Stocks>元素值是否是您所需要的。

输入 XML

<?xml version="1.0"?>
<ResultOfProductList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ServerCode>kenan</ServerCode>
    <ClientCode>B124565</ClientCode>
    <Username>karanfil</Username>
    <ClientIPAddress>78.135.235.3</ClientIPAddress>
    <Successful>true</Successful>
    <RequestDate>2022-07-22T12:28:54.6238843+03:00</RequestDate>
    <ElapsedTimeMS>17</ElapsedTimeMS>
    <MethodType>GetAllProductsByParts</MethodType>
    <Result EndOfProducts="false">
        <CustomerCode>B124565</CustomerCode>
        <CustomerName>kenan</CustomerName>
        <Date>2022-07-22T12:28:54.6238843+03:00</Date>
        <Brands>
            <Brand ID="174" Lang="tr" BrandName="PARTSMALL-KORE" StandardName=""/>
        </Brands>
        <Products>
            <Product ID="134898" BrandID="174" ProductCode="KR-PML-PTA-086" ProducerCode="" MinOrderAmount="1" PiecesInBox="1" Unit="PCE" New="false">
                <ProductNames>
                    <ProductName Lang="tr">VITES HALATI ( HYUNDAI : ACCENT 95-00 )</ProductName>
                </ProductNames>
                <BaseOeNr>43794-22000</BaseOeNr>
                <Pricing>
                    <ListPriceCurrency>USD</ListPriceCurrency>
                    <LocalCurrency>TLY</LocalCurrency>
                    <CurrencyRate>17.6599</CurrencyRate>
                    <ListPriceWoVAT>31.24</ListPriceWoVAT>
                    <LocalListPriceWVat>651.0004</LocalListPriceWVat>
                    <LocalListPriceWoVat>551.695251</LocalListPriceWoVat>
                    <LocalNetPriceWVat>377.580261</LocalNetPriceWVat>
                    <LocalNetPriceWoVat>319.983246</LocalNetPriceWoVat>
                    <Discount1>42</Discount1>
                    <Discount2>0</Discount2>
                    <Discount3>0</Discount3>
                    <Discount4>0</Discount4>
                    <Discount5>0</Discount5>
                    <Discount6>0</Discount6>
                    <InDiscount>false</InDiscount>
                </Pricing>
                <Stocks>
                    <Stock WarehouseID="1" Equality="Eq">0</Stock>
                    <Stock WarehouseID="7" Equality="Eq">0</Stock>
                    <Stock WarehouseID="4" Equality="Eq">0</Stock>
                    <Stock WarehouseID="2" Equality="Eq">0</Stock>
                    <Stock WarehouseID="5" Equality="Eq">0</Stock>
                </Stocks>
            </Product>
        </Products>
    </Result>
</ResultOfProductList>

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/ResultOfProductList">
        <Products>
            <xsl:for-each select="Result/Products/Product">
                <Product>
                    <ID>
                        <xsl:value-of select="@ID"/>
                    </ID>
                    <BrandID>
                        <xsl:value-of select="@BrandID"/>
                    </BrandID>
                    <BaseOeNr>
                        <xsl:value-of select="BaseOeNr"/>
                    </BaseOeNr>
                    <ProductCode>
                        <xsl:value-of select="@ProductCode"/>
                    </ProductCode>
                    <ProductName>
                        <xsl:value-of select="ProductNames/ProductName"/>
                    </ProductName>
                    <LocalCurrency>
                        <xsl:value-of select="Pricing/LocalCurrency"/>
                    </LocalCurrency>
                    <LocalNetPriceWVat>
                        <xsl:value-of select="Pricing/LocalNetPriceWVat"/>
                    </LocalNetPriceWVat>
                    <Stocks><xsl:value-of select="sum(Stocks/Stock)"/></Stocks>
                </Product>
            </xsl:for-each>
        </Products>
    </xsl:template>
</xsl:stylesheet>

输出 XML

<Products>
  <Product>
    <ID>134898</ID>
    <BrandID>174</BrandID>
    <BaseOeNr>43794-22000</BaseOeNr>
    <ProductCode>KR-PML-PTA-086</ProductCode>
    <ProductName>VITES HALATI ( HYUNDAI : ACCENT 95-00 )</ProductName>
    <LocalCurrency>TLY</LocalCurrency>
    <LocalNetPriceWVat>377.580261</LocalNetPriceWVat>
    <Stocks>0</Stocks>
  </Product>
</Products>

C#

void Main()
{
   const string SOURCEXMLFILE = @"e:\Temp\input.xml";
   const string XSLTFILE = @"e:\Temp\process.xslt";
   const string OUTPUTXMLFILE = @"e:\temp\output.xml";

   try
   {
      XsltArgumentList xslArg = new XsltArgumentList();

      using (XmlReader src = XmlReader.Create(SOURCEXMLFILE))
      {
         XslCompiledTransform xslt = new XslCompiledTransform();
         xslt.Load(XSLTFILE, new XsltSettings(true, true), new XmlUrlResolver());

         XmlWriterSettings settings = xslt.OutputSettings.Clone();
         settings.IndentChars = "\t";
         // to remove BOM
         settings.Encoding = new UTF8Encoding(false);

         using (XmlWriter result = XmlWriter.Create(OUTPUTXMLFILE, settings))
         {
            xslt.Transform(src, xslArg, result, new XmlUrlResolver());
            result.Close();
         }
      }
      Console.WriteLine("File '{0}' has been generated.", OUTPUTXMLFILE);
   }
   catch (Exception ex)
   {
      Console.WriteLine(ex.Message);
   }
}

暂无
暂无

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

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