如何使用C#在XML文件中添加新记录 [英] How to add new record in XML file with C#

查看:153
本文介绍了如何使用C#在XML文件中添加新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre lang="c++"><pre lang="c#">



我面临着在我的xml文件中添加新记录的问题。

假设我有这个plist.xml文件:



Hi,
I am facing problem about adding new record on my xml file.
Let's say I have this plist.xml file:

<Project>
  <ProjectDetail id="0">
    <title>Batman vs Superman</title>
    <platform>TV OS</platform>
    <code>5504</code>
    <dbname>BVSM</dbname>
  </ProjectDetail>
</Project>





我想要的是在该XML文件上添加新记录:

- ProjectDetail id =1

- title = Cartman and Stan

- 平台=电视操作系统

- 代码= 1045

- dbname = CAS



所以它看起来像这样:



What I want is to add new record on that XML file:
- ProjectDetail id = "1"
- title = Cartman and Stan
- platform = TV OS
- code = 1045
- dbname = CAS

So it would looks like this:

<Project>
  <ProjectDetail id="0">
    <title>Batman vs Superman</title>
    <platform>TV OS</platform>
    <code>5504</code>
    <dbname>BVSM</dbname>
  </ProjectDetail>
<ProjectDetail id="1">
    <title>Cartman and Stan</title>
    <platform>TV OS</platform>
    <code>1048</code>
    <dbname>CAS</dbname>
  </ProjectDetail>
</Project>





任何人都可以给我见解吗?



我尝试过:



我搜索了很多东西,但由于这是我第一次使用XML工作,我很困惑从哪里开始。



我试过这个



XmlElement elem = doc.CreateElement(ProjectDetail);

elem.InnerText = 1;

doc.CreateElement(title);

elem.InnerText =Cartman and Stan;



root.AppendChild(elem);

doc.Save(frm1.filedata);



但结果是错误的。



Anyone can give me insight?

What I have tried:

I googled so many things, but since this is my first work with XML I confuse where to start.

I tried this

XmlElement elem = doc.CreateElement("ProjectDetail");
elem.InnerText = "1";
doc.CreateElement("title");
elem.InnerText = "Cartman and Stan";

root.AppendChild(elem);
doc.Save(frm1.filedata);

But the result is wrong.

推荐答案

虽然主要使用 Xdocument ,但我认为在你的情况下 Xelement 更容易使用,以下是一些示例: XElement类概述 [ ^ ]



Although Xdocument is mostly used, I think in your case Xelement is easier to use, here are some examples: XElement Class Overview[^]

XElement contacts =
new XElement("Contacts",
    new XElement("Contact",
        new XElement("Name", "Patrick Hines"), 
        new XElement("Phone", "206-555-0144"),
        new XElement("Address",
            new XElement("Street1", "123 Main St"),
            new XElement("City", "Mercer Island"),
            new XElement("State", "WA"),
            new XElement("Postal", "68042")
        )
    )
);







// This is an WPF example !
private XElement xmlSource;




// Set the datagrid source to XML file.
this.xmlSource = XElement.Load(this.fileName);
this.DataGrid1.DataContext = this.xmlSource;




this.xmlSource.Save(this.fileName);


引用:

我混淆了从哪里开始。

I confuse where to start.

从Google开始并搜索XLM读/写库。

随意阅读文档并按照教程进行操作。

抱歉没时间搜索你。

Start with Google and search an XLM reader/writer library.
Feel free do read documentation and follow tutorials too.
Sorry no time to do search for you.


实现这一点的方法很少:

1)使用System.Xml.Linq类: XDocument [ ^ ] + XElement [ ^ ] + XAttribute [ ^ ]:

There's few ways to achieve that:
1) using System.Xml.Linq classes: XDocument[^] + XElement[^] + XAttribute[^]:
string xcontent = @"<Project>
  <ProjectDetail id='0'>
    <title>Batman vs Superman</title>
    <platform>TV OS</platform>
    <code>5504</code>
    <dbname>BVSM</dbname>
  </ProjectDetail>
</Project>";

//just for example
XDocument xdoc = XDocument.Parse(xcontent);
//you should use:
//XDocument xdoc = XDocument.Load("FullFileName.xml");

XElement xele = new XElement("ProjectDetail", new XAttribute("id", 1),
	new XElement("title", "Cartman and Stan"),
	new XElement("platform", "TV OS"), 
	new XElement("code", 1048),
	new XElement("dbname", "CAS"));

xdoc.Root.Add(xele);
xdoc.Save("FullFileName.xml");





2)使用System.Xml类: XmlReader [ ^ ] + XmlWriter [ ^ ]



3)使用Xml序列化和反序列化

XML序列化简介 [ ^ ]

XML序列化示例 [ ^ ]

XML序列化和反序列化:第1部分 [ ^ ]

XML序列化和反序列化:第2部分 [ ^ ]

自定义类集合序列化和反序列化的完整示例 [ ^ ]



试试!



2) using System.Xml classes: XmlReader[^] + XmlWriter[^]

3) using Xml Serialization and Deserialization
Introducing XML Serialization[^]
Examples of XML Serialization[^]
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[^]

Try!


这篇关于如何使用C#在XML文件中添加新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆