0

我在隔离存储中的现有 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。

我没有像历史一样添加主根节点的问题,但是我如何使用 XMLTextWritter 编写意味着我将如何使用 Writer 添加历史根节点

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

2 回答 2

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>

这可能是你想要的。

于 2012-06-01T12:03:14.370 回答
0

嗨,最后我在也门和我的一些研发的帮助下完成了这件事。

这是我的代码片段,它适用于我......

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);
                    }
                }
            }
于 2012-06-02T16:33:07.027 回答