tags:

views:

255

answers:

2


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

+3  A: 
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());
Pavel Minaev
Great! I learn something new. I use this code but an error at Single - XElement doesnot contains a definition. How it can be resolved?
adatapost
Given the use of LINQ, may I suggest you also use "var" where possible?
Steven Sudit
adatapost, you need to include the right namespaces for LINQ to XML.
Steven Sudit
Thanks @Steven. I am sorry but I can't resovle this. May be it is belongs to .netframework 4.0
adatapost
Thanks ...where is [Single]'s definition?
@Steven, I don't see the need for `var` here - there are no anonymous types used, and outside of them, it's a matter of style.
Pavel Minaev
Regarding missing `Single()` - it should've been `Elements("filter")` rather than `Element("filter")` - I've fixed the answer accordingly.
Pavel Minaev
@adatapost: It's .NET 3.5.
Steven Sudit
@Pavel: It's not required by C#, so it would work either way, but using "var" complies with DRY. Essentially, I don't see any benefit to saying XDocument twice in a row. In short, I'd treat this as a matter of maintainability rather than style.
Steven Sudit
@Steven, if you've seen any discussion on use/misuse of `var` on C# public forums/newsgroups/blogs, you know that it is a very debated issue, and there's no clear consensus on it. Some say what you say; others believe that it hampers readability. Several major style guidelines forbid its use except when it's required by anonymous types; some only allow it when right hand side is `new T`. I've worked on projects that had it both ways, and prefer to stay out of this holywar :)
Pavel Minaev
@Pavel: You're right that there's some unnecessary heat on this topic. What I find absurd is that VB.NET has had the equivalent syntax and it's been uncontroversial.
Steven Sudit
+4  A: 

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"&gt;
<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.

Rex M
XslTransform is obsolete, use XslCompiledTransform. Syntax is identical.
Ty
+1 XLST is the good old-fashioned way to turn XML into text.
Steven Sudit
Ty: Compiling is faster, but has a high initial cost.
Steven Sudit
@Ty thanks, that's a good option too.
Rex M
<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.
Ty