读取/更新XML文档类 [英] Read/update XML document class

查看:149
本文介绍了读取/更新XML文档类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先抱歉,但是我是中继菜鸟,我尝试学习.

我有一个Xml文件,例如xmldoc.xml

sorry first, but I''m relay noob and I try to learn .

I have an Xml document like xmldoc.xml

<root>
    <one>first_one</one>
    <two>second_one</two>
    <tree>third_one</tree>
    <four>fourth_one</four>
    <five>fifth_one</five>
    <six>sixth_one</six>
</root>



我尝试过的事情:

我的问题是我尝试读取和/或更新根节点内的一个或多个元素.
老实说,我不知道怎么做.

我尝试使用序列化程序,但是我进行堆栈操作,因为我不知道如何创建类,就像我将尝试解释的那样:



What I have tried:

my problem is I try to read and/or update one or more elements inside my root node.
To be honest I don''t have a clue how.

I try serializer but I stack because don''t know to create class like I will try to explain:

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmldoc.xml);
XmlNode root = xmldoc.FirstChild;

//create select_array[] = { one, tree four}
//create array new_val[] = {one => "first_second", tree => "tree_second", four => "four_second" }

// 1. create a class read_array values of one, tree four
// 2. create class save_array_new values of one, tree four


我不知道以这种方式创建类读取或保存以更改一个,两个..或所有节点值

我写的只是使用数组的想法,但我认为不是很好.


I don''t know to create class read or save in this kind of way to can change one , two .. or all nodes values

up I write is only my idea to use array but i don''t think is good

推荐答案

这里是使用 XElement 的简单示例:
Here is a simple example using XElement:
            string TestXml = @"<root>
    <one>first_one</one>
    <two>second_one</two>
    <tree>third_one</tree>
    <four>fourth_one</four>
    <five>fifth_one</five>
    <six>sixth_one</six>
</root>";

            XElement x = XElement.Parse(TestXml);
            var one = x.Element("one");
            one.Value = "CHANGED ONE";
            x.Element("two").Remove();

            foreach (var item in x.Elements())
            {
                Debug.Print(item.ToString());
            }


如果您更喜欢使用 XDocument ,这没什么不同,您将必须记住使用根,请参见此处的说明:


If you prefer using XDocument, this is not much different, you will have to remember to use the root, see explanation here: Querying an XDocument vs. Querying an XElement (C#) | Microsoft Docs[^]
But as the MSDN documentation states:

报价:

在许多情况下,您的应用程序将不需要您创建文档.通过使用XElement类,可以创建XML树,向其添加其他XML树,修改XML树并保存.

In many cases, your application will not require that you create a document. By using the XElement class, you can create an XML tree, add other XML trees to it, modify the XML tree, and save it.

^ ]

请注意,使用.NET 3.0或更低版本时,您只需要 XmlDocument .

XDocument Class Overview (C#) | Microsoft Docs[^]

Note that you only need XmlDocument when using .NET version 3.0 or lower.


请参考MSDN文档:XmlDocument类(System.Xml)| Microsoft文档 [ ^ ],尤其是使用XPath导航选择节点 [
Please, refer MSDN documentation: XmlDocument Class (System.Xml) | Microsoft Docs[^], especially Find nodes[^] and Edit nodes.
You might be interested in Select Nodes Using XPath Navigation[^]


According to the OP requirements (posted in solution 3):
报价:

我想保持xml文件的原样
1.我唯一需要的是创建一个类,如读取一,二,树或所有值元素
2.一类更新一,二,树或所有值元素

I Want to keep the xml file like it is
1. The only think I need is to create class like read one, two, tree or all values elements
2. one class to update one, two, tree or all values elements



我建议将您的xml结构更改为此:



I''d recommend to change your xml structure to this one:

<MyItems>
    <MyItem id = "first">first one</Item>
    <MyItem id = "second">first one</Item>
    <MyItem id = "third">first one</Item>
    <MyItem id = "fourth">first one</Item>
    <!-- and so on -->
</MyItems>



表示单个项目的类可能类似于:



A class representing single item this may look like:

public class MyItem
{
	public string id = "first";
	public string text = "first_one";  
}



最后,您需要一个MyItem的集合.

有关更多详细信息,请阅读我的提示:
自定义的完整示例类集合序列化和反序列化 [ ^ ].没错,它是用Vb.net编写的,但是您必须处理它,因为Vb.net与C#非常相似.



Finally, you need a collection of MyItem.

For further details, please read my tip: A Complete Sample of Custom Class Collection Serialization and Deserialization[^]. True, it''s written in Vb.net, but you have to handle it, because Vb.net is pretty similar to C#.


这篇关于读取/更新XML文档类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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