如何更新Xml文件................... [英] How to update an Xml file ...................

查看:70
本文介绍了如何更新Xml文件...................的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在尝试创建一个文件,该文件可以存储一些信息,例如5个字段(X,X ChordNote越来越多...)
我做了这段代码,但是我不知道如何插入更多信息.

每个插入都称为Accordo
此插入内容有5个字段

如果我想使用button_click插入更多Information Accordo,该怎么办?

_________
这是我的代码:

Hello everybody,
I am trying to create a file which can store some informations for example 5 fields ( X, X ChordNote more and more... )
I did this code, But I do not know how to insert more informations.

Each Insert is called Accordo
this insert has 5 fields

How can I do if ,using button_click, I want to insert more Information Accordo?

_________
here is my code:

Dim dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
        Doc.AppendChild(dec)

        Dim DocRoot As XmlElement = Doc.CreateElement("Accordo")
        Doc.AppendChild(DocRoot)
        Dim ChordX As XmlNode = Doc.CreateElement("ChordX")
        ChordX.InnerText = "ChordX"
        DocRoot.AppendChild(ChordX)
        Dim ChordY As XmlNode = Doc.CreateElement("ChordY")
        ChordY.InnerText = "ChordY"
        DocRoot.AppendChild(ChordY)
        Dim ChordNota As XmlNode = Doc.CreateElement("ChordNota")
        ChordNota.InnerText = "ChordNota"
        DocRoot.AppendChild(ChordNota)
        Dim ChordModal As XmlNode = Doc.CreateElement("ChordModal")
        ChordModal.InnerText = "ChordModal"
        DocRoot.AppendChild(ChordModal)
        Dim ChordID As XmlNode = Doc.CreateElement("ChordID")
        ChordID.InnerText = "ChordID"
        DocRoot.AppendChild(ChordID)
        'DocRoot.AppendChild(ChordNota)
        Doc.Save("c:\OutDocument.xml"



已移动表单OP的答案":

那我该如何纠正呢?
我的意思是我的目标是获得一些简单的功能,例如:

Create_New创建一个新文件

Insert_New ro插入信息块(与字段有关)

有办法吗? DOM与否DOM
我不在乎方式,但结果
有人可以告诉我解决方案吗?

在此先非常感谢您



Moved form OP''s "answer":

So how can I correct it?
I mean my goal is to obtain few simple functions like:

Create_New to create a new file

Insert_New ro insert a block of informations ( related to fields )

is there a way? DOM or not DOM
I do not care the way but the result
Can someone show me the solution?

Thank you so much in advance

推荐答案

几乎在所有XML技术中,并且可以肯定的是,您现在正在使用DOM,请完整阅读XML,并更新对象(或整个对象图)存储在内存中(而不是XML),然后将其完整地保存在流中的XML中–覆盖同一文件或创建一个新文件.

使用DOM,您可以以与在代码中显示的方式完全相同的方式插入新节点-没什么不同.

但是,我认为您的问题是使用DOM本身作为数据模型,这会创建额外的间接级别.您可以更好地根据您的应用程序需求更好地创建数据模型(您对此有所了解,并且不会在答案中披露).在这种情况下,使用DOM会创建附加的映射层,而该映射层没有其他用途.

为了避免这种情况,我强烈建议您切换使用do DataContract方法.这样,您就应该忘记DOM,而实际上应该忘记XML — System.Runtime.Serialization.DataContractSerializer将为您完成所有工作,请参阅Microsoft对此主题的帮助.您只需要用属性[DataContract]标记所有类(不要忘记名称空间的唯一URL,使其成为世界唯一(!)),并用属性.在所有标准.NET方法中,这是最不介入的方法.这样,您将能够专注于数据结构,而不是流表示.

-SA
In nearly all XML techniques, and quite certainly with DOM you''re using right now, you read XML in full, update the object (or whole object graph) in memory (but not XML), and then save it in the XML in full in your stream -- overwrite the same file or create an new one.

With DOM, you insert new nodes exactly the same way you show in your code — no different.

However, I think you problem is using DOM itself as a data model, which create an extra level of indirection. You can better create the data model closely following your application needs (which you know better and don''t disclose in your answer). In this case, working with DOM would created additional — mapping layer, which serves not other purpose.

To go away from that, I would strongly recommend you to switch do DataContract approach. In this way, you should forget about DOM and, effectively, about XML — System.Runtime.Serialization.DataContractSerializer will do it all for you, see Microsoft help on the topic. You only need to mark all your classes with the attribute [DataContract] (don''t forget your unique URL for name space to make it world-unique (!)) and mark each data member you want to persist with the attribute [DataMember]. Among all standard .NET approaches this one is the most non-intrusive. This way, you will be able to focus on your data structures, not on stream representations.

—SA


我经常使用 ^ ]可以基于XML架构生成类库.生成的类可以与 Eclipse [ ^ ]具有不错的可视XML Schema编辑功能.

问候
Espen Harlinn
I often use the XML Schema Definition Tool (Xsd.exe)[^] to generate a class library based on an XML Schema, The generated classes can be used in conjunction with System.XML.Serialization.XMLSerializer[^] to read and write XML code that follows the schema.

Eclipse[^] has nice visual XML Schema editing features.

Regards
Espen Harlinn


这篇关于如何更新Xml文件...................的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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