2012-05-30 64 views
0

我沒有權利在磁盤上寫入。我想創建一個string,其中包含xml文件。如何在字符串中編寫XML?

我不想給文件名,因爲我沒有權限。如果我只是追加xmlstring它工作?有沒有更好的方法來做到這一點。

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

回答

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

您可以創建單元,節點和隨意屬性。

+0

我需要寫標記和值在XML。它是否適用於xmlWriter.WriteStartElement(「ChartSetting」);或xmlWriter.WriteAttributeString(「ChartName」,value); –

+0

從Microsoft文檔XmlElement elem = doc.CreateElement(「price」); XmlText text = doc.CreateTextNode(「19.95」); doc.DocumentElement.AppendChild(elem); doc.DocumentElement.LastChild.AppendChild(text); –

相關問題