3

Is there any way to generate the XElement representation in C# from a given XML string?

Basically what I want to achieve is going from a string like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0">
  <channel>
    <title>RSS Channel Title</title>
    <description>RSS Channel Description.</description>
    <link>http://aspiring-technology.com</link>
    <item>
      <title>First article title</title>
      <description>First Article Description</description>
    </item>
  </channel>
</rss>

To a string like this:

XDocument doc = new XDocument(
   new XDeclaration("1.0", "utf-8", "yes"),
   new XElement("rss", 
       new XAttribute("version", "2.0"),
       new XElement ("channel",
           new XElement("title", "RSS Channel Title"),
           new XElement("description", "RSS Channel Description."),
           new XElement("link", "http://aspiring-technology.com"),
           new XElement("item",
               new XElement("title", "First article title"),
               new XElement("description", "First Article Description")
           )
       )
    );

Really appreciate any hints!

1
  • As I understand you are trying to build a code generator. You could possibly mention a keyword "code generation" to avoid misleading answers. Commented Sep 6, 2012 at 18:25

2 Answers 2

3

ReSharper 2016.1 has a context action to convert string to XElement or XDocument object.

gif example how it works

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

1 Comment

Pls add that descriptive to view - you have to put "!" in front of the line.
1

Take a look at this XElement/XDocument Code Generator. It generates c# code from XML using XSLT transformation. If I would do it myself I'll probably do it the same way.

1 Comment

This seems to be very near to want I want to achieve. Thanks a lot!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.