1

in xml file

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

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

2
  • You mean hold this xml as string? Commented Sep 7, 2009 at 2:27
  • No, he means to turn the XML into a (near) csv equivalent. Commented Sep 7, 2009 at 2:29

2 Answers 2

4

There's a dozen different ways to do this. Here's a very simple example using 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:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

XslTransform is obsolete, use XslCompiledTransform. Syntax is identical.
Ty: Compiling is faster, but has a high initial cost.
<xsl:template match="item"> <xsl:value-of select="@name" />, <xsl:value-of select="@id" />, <xsl:value-of select="filter/@value" /> </xsl:template> This template should work as well.
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());

10 Comments

Great! I learn something new. I use this code but an error at Single - XElement doesnot contains a definition. How it can be resolved?
Given the use of LINQ, may I suggest you also use "var" where possible?
adatapost, you need to include the right namespaces for LINQ to XML.
Thanks @Steven. I am sorry but I can't resovle this. May be it is belongs to .netframework 4.0
Thanks ... where is [Single]'s definition?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.