0

ディスクに書き込む権利がありません。ファイルstringで構成されたを作成したい。xml

権限がないのでファイル名を付けたくありません。単に追加xmlした場合string、それは機能していますか?それを行うためのより良い方法はありますか?

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

4

2 に答える 2

1

コード入力:

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

テキスト出力:

<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>
于 2012-05-30T10:23:24.513 に答える
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.

MSドキュメントからのDOM操作の例:

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

要素、ノード、属性は自由に作成できます。

于 2012-05-30T08:45:47.907 に答える