且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

保存到xml文件并从中加载

更新时间:2023-11-02 12:52:22

我找不到AddNode方法,但是我可以在您的AddTreeNode中看到它方法,您不仅要添加它而且要递归.您是否已遍历代码以查看发生了什么?您是说您的XML有多个级别,但是它们都放在同一级别上吗?听起来您需要在AddTreeNode方法中使用断点以查看发生了什么.同样,制作文本OuterXml意味着使单个节点等于从此以后的所有XML.您肯定只需要该节点的文本吗?
I can''t find the AddNode method, but I can see that in your AddTreeNode method, you add it as well as recursing. Have you stepped through the code to see what''s happening ? Are you saying your XML has multiple levels but they all get put on at the same level ? Sounds like you need a breakpoints in your AddTreeNode method to see what is going on. Also, making the text OuterXml, means making a single node equal to all the XML from there down. You want the text of just that node, surely ?


确定,我删除了您的回复"并将其粘贴到您的问题中.首先,XmlNode是基类.您的节点是XmlElement,它具有您需要的属性.除此之外,我并没有真正了解问题所在,XML是否能反映您的期望?是阅读或写作方面的问题吗?如果是书面形式,也许是您发布了XML以及有关它未显示所需内容的解释?
OK, I deleted your ''reply'' and pasted it in to your question. First of all, XmlNode is a base class. Your node is an XmlElement, which has the properties you need. Beyond that, I''m not really following what''s going wrong, does the XML reflect what you expect it to ? Is the problem in reading or writing ? If it''s in writing, perhaps if you posted the XML as well as an explanation of how it''s not showing what you want it to ?


确定要减少吗?级别的XML编程?该问题已经为您很好地解决了.

我建议您改用数据合同.它的序列化器将自动存储和还原所有数据,不仅是树,还包括任意结构的对象图(这意味着它可以包含循环引用,序列化器将正确处理).另外,XML代码非常易于阅读,模式可以是世界唯一的(为此目的,请使用基于公司网站的名称空间),并且该技术是最不介入的—您只需向数据类型和成员添加属性(通常是[DataContract][DataMember]).

请参阅: http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

另请参阅我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [
Are you sure you need low-level XML programming? The problem is already nicely solved for you.

I would suggest you use Data Contract instead. Its serializer will automatically store and restore all your data, not just tree, but an object graph of the arbitrary structure (it means, it can contain circular references, the serializer will handle it correctly). Also, the XML code is very human-readable, the schema can be world-unique (use your company site based name space for this purpose) and the technique is the most non-intrusive — you only add attributes to the data types and members (usually [DataContract] and [DataMember]).

See: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

See also my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating a property files...[^].

—SA