读取和写入XML数据

时间:2021-11-06 23:10:58

I have been tinkering with this project for a while and I've run into a brick wall. This is my very first project and I'm really not sure where to go from here. I was trying my hardest to read up on what the next step is so I wouldn't have to post on here again, but it seems I have little choice.

我一直在修补这个项目,我遇到了一堵砖墙。这是我的第一个项目,我真的不知道从哪里开始。我正在努力阅读下一步是什么,所以我不必再在这里发帖,但似乎我别无选择。

Anyway, here is a brief explanation on what I am trying to accomplish with this project. I am trying to retrieve certain values from three elements in an already existing XML document. After I have loaded each value for each element into it's respective text box, I am then trying to save any changes to the values into the document. (Easier said that done!)

无论如何,这里有一个简短的解释,我想用这个项目来完成什么。我试图从已经存在的XML文档中的三个元素中检索某些值。在我将每个元素的每个值加载到它的相应文本框后,我然后尝试将值的任何更改保存到文档中。 (更容易说完了!)

I am using XDocument to store the values into a list and then display them to their text boxes.

我正在使用XDocument将值存储到列表中,然后将它们显示到文本框中。

I can't figure out how to update changes back to the original values and save. So far trying to save leaves me with a blank XML document and crashes my application. :\

我无法弄清楚如何将更改更新回原始值并保存。到目前为止,试图保存一个空白的XML文档并使我的应用程序崩溃。 :\

Here is the XML data that I can read and display:

这是我可以阅读和显示的XML数据:

<client>
  <endpoint address="http://127.0.0.1:8086">
  <endpoint address="http://127.0.0.1:8084">
  <endpoint address="net.tcp://127.0.0.1:8085">
</client>

And here is some of the code that I have written thus far.

这是我到目前为止编写的一些代码。

    OpenFileDialog AgentConfig = new OpenFileDialog();

    private void button1_Click(object sender, EventArgs e)
    {

        AgentConfig.Filter = "Agent.exe.config (*.config)|*.config";
        if (AgentConfig.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = AgentConfig.FileName;
        }

        var addresses = XDocument.Load(AgentConfig.FileName)
                     .Descendants("endpoint")
                     .Select(x => (string)x.Attribute("address"))
                     .ToList();

        textBox2.Text = addresses[0];
        textBox3.Text = addresses[1];
        textBox4.Text = addresses[2];

        if (textBox2.Text != addresses[0])
        {
            addresses[0] = textBox2.Text;
        }

        if (textBox3.Text != addresses[1])
        {
            addresses[1] = textBox3.Text;
        }

        if (textBox4.Text != addresses[2])
        {
            addresses[3] = textBox4.Text;
        }


    }

    private void button2_Click(object sender, EventArgs e)
    {
        SaveFileDialog SF = new SaveFileDialog();
        if (SF.ShowDialog() == DialogResult.OK)
        {

        }
    }

Any help would definitely be appreciated.

任何帮助肯定会受到赞赏。

Thanks in advance!

提前致谢!

3 个解决方案

#1


1  

var xElem = new XElement("client",
    new XElement("endpoint", new XAttribute("address", textBox2.Text)),
    new XElement("endpoint", new XAttribute("address", textBox3.Text)),
    new XElement("endpoint", new XAttribute("address", textBox4.Text)));

xElem.Save(filename);

#2


0  

One approach could be to use the following classes:

一种方法可以是使用以下类:

class System.Data.DataSet 

Represents an in-memory cache of data, see documentation

表示内存中的数据缓存,请参阅文档

class System.IO.StramWriter  

Implements a TextWriter for writing characters to a stream in a particular encoding, see documentation

实现TextWriter以将字符写入特定编码的流中,请参阅文档

Go then like this:

那么就这样:

DataSet ds = newDataSet();
CreateMyDataSet("your arguments"); // Create your DataSet according to your xml-format
StreamWriter sw = new StreamWriter(SaveFileDialog.FileName, ...);
sw.Write(ds.GetXml());  // GetXml() returns the xml representation of your data
sw.Close();  

#3


-1  

try Using a Xml writer to save. Here is a link fot you

尝试使用Xml编写器进行保存。这是你的链接

[a link]http://www.dotnetperls.com/xmlwriter

#1


1  

var xElem = new XElement("client",
    new XElement("endpoint", new XAttribute("address", textBox2.Text)),
    new XElement("endpoint", new XAttribute("address", textBox3.Text)),
    new XElement("endpoint", new XAttribute("address", textBox4.Text)));

xElem.Save(filename);

#2


0  

One approach could be to use the following classes:

一种方法可以是使用以下类:

class System.Data.DataSet 

Represents an in-memory cache of data, see documentation

表示内存中的数据缓存,请参阅文档

class System.IO.StramWriter  

Implements a TextWriter for writing characters to a stream in a particular encoding, see documentation

实现TextWriter以将字符写入特定编码的流中,请参阅文档

Go then like this:

那么就这样:

DataSet ds = newDataSet();
CreateMyDataSet("your arguments"); // Create your DataSet according to your xml-format
StreamWriter sw = new StreamWriter(SaveFileDialog.FileName, ...);
sw.Write(ds.GetXml());  // GetXml() returns the xml representation of your data
sw.Close();  

#3


-1  

try Using a Xml writer to save. Here is a link fot you

尝试使用Xml编写器进行保存。这是你的链接

[a link]http://www.dotnetperls.com/xmlwriter