使用类和类数据生成XML [英] Using a class and class Data to Genereate XML

查看:111
本文介绍了使用类和类数据生成XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我从我拥有的类创建xml时有一些疑问.我需要在本地文件系统中生成一个xml文件,该xml的节点和数据应该从第一次生成一次.数据中发生的任何更改应使用以下某种逻辑保存在本地文件系统中生成的XML中.

Hi all,

i have some doubts in creating xml from a class that i have. I need to grenerate a xml file in my local filesystem the Nodes and Data of a xml should be Generated once from the first time. What ever changed made in the data should be saved in the genereated XML in my local file system using the logic some thing as below.

XmlDocument doc = new XmlDocument();
           doc.LoadXml("<item><name>wrench</name></item>");

           // Add a price element.
           XmlElement newElem = doc.CreateElement("price");
           newElem.InnerText = "10.95";
           doc.DocumentElement.AppendChild(newElem);

           // Save the document to a file. White space is
           // preserved (no white space).
           doc.PreserveWhitespace = true;

           doc.Save("" + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + UserPrincipal.Current.DisplayName.Replace(' ', '_') + ".xml");



上面的例子是我从MSDN获得的,它工作正常.我需要的是在LoadXML中或使用其他一些方法,我需要使用自定义类创建XML.



The above example is what i got from MSDN which works fine. what i need is in the LoadXML or using some other method i need to create a XML Using my Custom Class.

推荐答案

我想您想要的是直接将XML序列化的XML存储对象的结构.有关更多信息,请参见此处:
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization [ ^ ]
http://support.microsoft.com/kb/815813/en-us [ ^ ]
I guess what you want is XML Serialization to directly store the structure of your objects. See here for more information:
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization[^]
http://support.microsoft.com/kb/815813/en-us[^]


这篇关于使用类和类数据生成XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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