2012-06-01 96 views
0

將添加一些節點添加到隔離存儲中的現有XML。將節點添加到隔離存儲中的現有XML

using (Stream stream = myIsolatedStorage.OpenFile("History.xml", FileMode.Open, FileAccess.ReadWrite)) 
         { 
          XDocument loadedData = XDocument.Load(stream); 

          var RootNode = new XElement("History"); 
          var RecipentN = new XElement("RecipentName", "ABC"); 
          var RecipentNo = new XElement("RecipentNumber", "ABABAB"); 
          var Time = new XElement("TimeStamp", "Monday"); 
          var MessageBody = new XElement("Message", "23"); 
          RootNode.Add(RecipentN, RecipentNo, Time, MessageBody); 
          loadedData.Root.Add(RootNode); 
          loadedData.Save(stream); 
         } 
        } 

我想我的XML會是這樣

<?xml version="1.0" encoding="utf-8"?> 
<History> 
    <RecipentName>Arslan</RecipentName> 
    <RecipentNumber>03336329631</RecipentNumber> 
    <TimeStamp>123456</TimeStamp> 
    <Message>ABC</Message> 
</History 

<History> 
    <RecipentName>Arslanaa</RecipentName> 
    <RecipentNumber>03336329631</RecipentNumber> 
    <TimeStamp>123456</TimeStamp> 
    <Message>ABC</Message> 
</History 

但我的代碼合併到現有的XML錯誤

<?xml version="1.0" encoding="utf-8"?> 
<History> 
    <RecipentName>Arslan</RecipentName> 
    <RecipentNumber>03336329631</RecipentNumber> 
    <TimeStamp>123456</TimeStamp> 
    <Message>ABC</Message> 
    <History> 
    <RecipentName>Java</RecipentName> 
    <RecipentNumber>John</RecipentNumber> 
    <TimeStamp>23</TimeStamp> 
    <Message>23</Message> 
    </History> 
</History> 

誰能請幫助我,我怎麼能節點,以現有的XML哪在Isolatedstorage中。

我有沒有問題要添加主根節點喜歡讀史,但後來我怎麼能寫操作和XMLTextWritter意味着我將如何添加史根節點與作家

    writer.WriteStartElement("History", ""); 

        writer.WriteStartElement("RecipentName", ""); 
        writer.WriteString("Arslan"); 
        writer.WriteEndElement(); 

        writer.WriteStartElement("RecipentNumber", ""); 
        writer.WriteString("03336329631"); 
        writer.WriteEndElement(); 

        writer.WriteStartElement("TimeStamp", ""); 
        writer.WriteString("123456"); 
        writer.WriteEndElement(); 

        writer.WriteStartElement("Message", ""); 
        writer.WriteString("ABC"); 
        writer.WriteEndElement(); 

        // Ends the document 
        writer.WriteEndDocument(); 
        writer.flush(); 

回答

1

嗨,最後我在也門和我的一些R的幫助下搗亂了這件事& D。

這裏是我的代碼片段,對我的作品......

if (!myIsolatedStorage.FileExists("History.xml")) 
       { 
        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("History.xml", FileMode.Create, myIsolatedStorage)) 
        { 
         XmlWriterSettings settings = new XmlWriterSettings(); 
         settings.Indent = true; 
         using (XmlWriter writer = XmlWriter.Create(isoStream, settings)) 
         { 
          writer.WriteStartElement("History", ""); 
          writer.WriteStartElement("SmSHistory", ""); 

          writer.WriteStartElement("RecipentName", ""); 
          writer.WriteString(RecipentName); 
          writer.WriteEndElement(); 

          writer.WriteStartElement("RecipentNumber", ""); 
          writer.WriteString(RecipentNumber); 
          writer.WriteEndElement(); 

          writer.WriteStartElement("TimeStamp", ""); 
          writer.WriteString(TimeStamp); 
          writer.WriteEndElement(); 

          writer.WriteStartElement("Message", ""); 
          writer.WriteString(Message); 
          writer.WriteEndElement(); 

          // Ends the document 
          writer.WriteEndDocument(); 

          // Write the XML to the file. 
          writer.Flush(); 
         } 
        } 
       } 

       else 
       { 
        XDocument loadedData; 
        using (Stream stream = myIsolatedStorage.OpenFile("History.xml", FileMode.Open, FileAccess.ReadWrite)) 
        { 
         // Load History.xml From Isolated Storage 
         loadedData = XDocument.Load(stream); 

         // Add SMS History Tags to XMLDocument 
         var RootNode = new XElement("SmSHistory"); 
         var RecipentN = new XElement("RecipentName", RecipentName); 
         var RecipentNo = new XElement("RecipentNumber", RecipentNumber); 
         var Time = new XElement("TimeStamp", TimeStamp); 
         var MessageBody = new XElement("Message", Message); 
         RootNode.Add(RecipentN, RecipentNo, Time, MessageBody); 

         // Find Root Element And Descedents and Append New Node After That 
         var root = loadedData.Element("History"); 
         var rows = root.Descendants("SmSHistory"); 
         var lastRow = rows.Last(); 
         lastRow.AddAfterSelf(RootNode); 
        } 

        // Save To History.xml File 
        using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("History.xml", FileMode.Create, myIsolatedStorage)) 
        { 
         loadedData.Save(myStream); 
        } 
       } 
      } 
1

你的目標XML是無效的,因爲沒有根節點。你在同一級別有兩個History節點。

,如果您有一個新的根快樂(比如說Histories),你可以這樣做:

// load original XML from the stream 
XDocument loadedData = XDocument.Load(stream); 

// create a new parent XML structure (new root) and load the original nodes 
var newXml = new XDocument(new XElement("Histories")); 
newXml.Root.Add(loadedData.Root); 

// create the new node 
var NewNode = new XElement("History"); 
var RecipentN = new XElement("RecipentName", "ABC"); 
var RecipentNo = new XElement("RecipentNumber", "ABABAB"); 
var Time = new XElement("TimeStamp", "Monday"); 
var MessageBody = new XElement("Message", "23"); 
NewNode.Add(RecipentN, RecipentNo, Time, MessageBody); 

// add the new node 
newXml.Root.Add(NewNode); 

// save the stream 
newXml.Save(stream); 

這給了你:

<Histories> 
    <History> 
    <RecipentName>Arslan</RecipentName> 
    <RecipentNumber>03336329631</RecipentNumber> 
    <TimeStamp>123456</TimeStamp> 
    <Message>ABC</Message> 
    </History> 
    <History> 
    <RecipentName>ABC</RecipentName> 
    <RecipentNumber>ABABAB</RecipentNumber> 
    <TimeStamp>Monday</TimeStamp> 
    <Message>23</Message> 
    </History> 
</Histories> 

這可能是你想要的。

+0

有沒有其他的方法呢? –

+0

我相信還有很多其他的方法。但是你想要的方式(在頂層有兩個'History'節點)是無效的XML。如果你想要有無效的XML,你可以寫到文件流的末尾,但我認爲這是一個壞主意。 – yamen