将xml值转换为字符串(使用c#)

时间:2022-05-24 21:46:21

in xml file

在xml文件中

<dummy1>
  <dummy2>
    <dummy3>
      <items>
        <item id="1111" name="Real_item_Name" url="i=1111">
          <filter name="itemLevel" value="item_value"/>
          <filter name="source" value="dummy4"/>
        </item>
       <item id="2222" name="Real_item_Name2" url="i=222">
          <filter name="itemLevel" value="item_value2"/>
          <filter name="source" value="dummy5"/>
        </item>
              //roop 
      </items>
    </dummy3>
  </dummy2>
</dummy1>

how can i make this value in c# (insert String value)

如何在c#中设置这个值(插入字符串值)

Real_item_Name , 1111 , item_value
Real_item_Name2 , 2222 , item_value2
Real_item_Name3 , 3333 , item_value3

Real_item_Name, 1111, item_value Real_item_Name2, 2222, item_value2 Real_item_Name3, 3333, item_value3

please show me dom or sax example ...

请向我展示dom或sax示例…

2 个解决方案

#1


4  

There's a dozen different ways to do this. Here's a very simple example using XSL:

有十多种不同的方法可以做到这一点。下面是一个使用XSL的非常简单的示例:

mytransform.xsl:

mytransform.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:for-each select="items/item">
        <xsl:value-of select="@name" />, <xsl:value-of select="@id" />, <xsl:value-of select="filter/@value" />
    </xsl:for-each>
</xsl:template>

We load the XSL file into a transform object; specify an input XML and an output text file:

我们将XSL文件加载到转换对象中;指定输入XML和输出文本文件:

XslTransform xslt = new XslTransform();
xslt.Load("c:\\path\\mytransform.xsl");
xslt.Transform("c:\\path\\input.xml", "c:\\path\\output.txt");

Check out the documentation on XslTransform for more in-depth usage, like working in memory streams and XML objects instead of file paths. This demonstrates the core concepts though.

查看关于xsl转换的文档,以便更深入地使用,比如在内存流和XML对象中工作,而不是在文件路径中工作。这展示了核心概念。

#2


3  

XDocument xml = XDocument.Load("foo.xml");
string csv = string.Join("\n",
    xml.Descendants("item").Select(item =>
        string.Format("{0}, {1}, {2}",
            (string)item.Attribute("name"),
            (string)item.Attribute("id"),
            (string)item.Elements("filter")
                        .Single(f => f.Attribute("name") == "itemLevel")
                        .Attribute("value")))
       .ToArray());

#1


4  

There's a dozen different ways to do this. Here's a very simple example using XSL:

有十多种不同的方法可以做到这一点。下面是一个使用XSL的非常简单的示例:

mytransform.xsl:

mytransform.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:for-each select="items/item">
        <xsl:value-of select="@name" />, <xsl:value-of select="@id" />, <xsl:value-of select="filter/@value" />
    </xsl:for-each>
</xsl:template>

We load the XSL file into a transform object; specify an input XML and an output text file:

我们将XSL文件加载到转换对象中;指定输入XML和输出文本文件:

XslTransform xslt = new XslTransform();
xslt.Load("c:\\path\\mytransform.xsl");
xslt.Transform("c:\\path\\input.xml", "c:\\path\\output.txt");

Check out the documentation on XslTransform for more in-depth usage, like working in memory streams and XML objects instead of file paths. This demonstrates the core concepts though.

查看关于xsl转换的文档,以便更深入地使用,比如在内存流和XML对象中工作,而不是在文件路径中工作。这展示了核心概念。

#2


3  

XDocument xml = XDocument.Load("foo.xml");
string csv = string.Join("\n",
    xml.Descendants("item").Select(item =>
        string.Format("{0}, {1}, {2}",
            (string)item.Attribute("name"),
            (string)item.Attribute("id"),
            (string)item.Elements("filter")
                        .Single(f => f.Attribute("name") == "itemLevel")
                        .Attribute("value")))
       .ToArray());