0

I do not have right to write on a disk. I want to create a string which consist of xml file.

I do not want to give filename because I have no permissions. If I simply append xml to string is it working? Is there any better way to do it.

XmlTextWriter xmlWriter = new XmlTextWriter(fileName, Encoding.UTF8);

2 Answers 2

1

Code Input :

var rss = new XElement("rss", new XAttribute("version", "2.0"));
var channel = new XElement("channel",
    new XElement("title", "Liftoff News"),
    new XElement("link", "http://liftoff.msfc.nasa.gov/"),
    new XElement("description", "Liftoff to Space Exploration.")
    );
rss.Add(channel);
channel.Add(new XElement("item",
    new XElement("title", "Star City"),
    new XElement("link", "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp"),
    new XElement("description", @"
        How do Americans get ready to work with Russians aboard the
        International Space Station? They take a crash course in culture, language
        and protocol at Russia's Star City.
        "),
    new XElement("pubDate", DateTime.UtcNow),
    new XElement("guid", "http://liftoff.msfc.nasa.gov/2003/06/03.html#item573")
));

var text = rss.ToString();

Text Output:

<rss version="2.0">
  <channel>
    <title>Liftoff News</title>
    <link>http://liftoff.msfc.nasa.gov/</link>
    <description>Liftoff to Space Exploration.</description>
    <item>
      <title>Star City</title>
      <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
      <description>
          How do Americans get ready to work with Russians aboard the
          International Space Station? They take a crash course in culture, language
          and protocol at Russia's Star City.
      </description>
      <pubDate>2012-05-30T10:21:14.014Z</pubDate>
      <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
    </item>
  </channel>
</rss>
Sign up to request clarification or add additional context in comments.

Comments

0
XmlDocument myDocument = new XmlDocument();

myDocument.Load("XML STRING GOES HERE");

Do the manipulation of your xml document here

string output = myDocument.InnerXml //to get to the raw Xml string.

An example of the DOM manipulation from the MS documentation:

XmlElement elem = doc.CreateElement("price");
XmlText text = doc.CreateTextNode("19.95");
doc.DocumentElement.AppendChild(elem);
doc.DocumentElement.LastChild.AppendChild(text);

You may create elements, nodes and attributes at will.

2 Comments

I need to write tags and values in xml. Is it working for xmlWriter.WriteStartElement("ChartSetting"); or xmlWriter.WriteAttributeString("ChartName", value);
From the Microsoft documentation XmlElement elem = doc.CreateElement("price"); XmlText text = doc.CreateTextNode("19.95"); doc.DocumentElement.AppendChild(elem); doc.DocumentElement.LastChild.AppendChild(text);

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.