xml序列化器编写 [英] xml serializer writing

查看:71
本文介绍了xml序列化器编写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



使用下面的代码即可创建xml文件。我的要求是创建/更新XML文件,如果项目已经存在,那么我需要更新项目,否则我需要添加该元素。

Hello,

Using below code im creating the xml file. My requirement is to create/update XML file , if item already exists then i need to update the item else i need to add that element.

XmlSerializer serializer = new XmlSerializer(typeof(ReleaseChangeModel));
Stream fs = new FileStream(filename, FileMode.OpenOrCreate);
XmlWriter writer = new XmlTextWriter(fs, Encoding.Unicode);				
serializer.Serialize(writer, objRelchg);                
writer.Close();



以下是通过模型后从上面的代码生成的xml文件



http://i58.tinypic.com/2a9bfrp.png [ ^ ]



任何帮助都非常值得赞赏。



谢谢和问候,

穆罕默德


following is xml file generated from the above code after passing Model

http://i58.tinypic.com/2a9bfrp.png[^]

Any help is highly apprecicated.

Thanks and Regards,
Mohammed

推荐答案

你知道吗?将XML文件读入对象(从XML创建XmlDocument ...或DOM ...)?这就是改变特定财产或国家所需要做的事情;然后,你需要将更改后的DOM保存回文件以更新文件。



我建议你先从对象上做一些基本的研究-XML和XML到对象; CodeProject上有几篇好文章,包括:[ ^ ]。



本CP文章刚刚于本月发布:[ ^ ]。不幸的是,本文未涉及DataContract的使用,这是最新的(包含在.NET 3.0及更高版本中),以及imho,将对象移入和移出XML的最佳方式:[ ^ ]。
Do you know how to read an XML file into "objects" (create a XmlDocument ... or DOM ... from the XML) ? That's what you will need to do to change a specific property or state; and then, you'll need to save the changed DOM back to the file to update the file.

I suggest you start by doing some basic reasearch on Objects-to-XML and XML-to-Objects; there are several good articles here on CodeProject, including: [^].

This CP article has just been published this month: [^]. Unfortunatenly this article does not cover the use of DataContract, which is the newest (included in .NET 3.0 and later), and, imho, best way to move objects to and from XML: [^].


非常感谢您的回复。我检查了他们有用的链接。但我的要求类似于登录文件。当前代码中的xml写入存在一些问题。第一次它会写,第二次,如果我尝试用xml写标题,它写的是< root> element.I使用了简单的xmlreader和xmlwriter函数来完成当前代码。



< img src =http:// i57 .tinypic.com / 10crq7s.pngborder =0alt =图片和视频托管由TinyPic>< / img>



我的xml编写代码如下



Thanks alot for your kind reply. i have checked the links they are helpful. But My requirement is similar to logging into file. There is some problem in xml writing in the current code. First time it will write, second time if I try to write the title in xml, it is writing out of <root> element.I have used simple xmlreader and xmlwriter functions to accomplish in the present code.

<img src="http://i57.tinypic.com/10crq7s.png" border="0" alt="Image and video hosting by TinyPic"></img>

My xml write code is as below

using (StringWriter sw = new StringWriter(sbuilder))
{
    using (XmlTextWriter w = new XmlTextWriter(sw))
    {
        w.WriteStartElement("item");
    }
}


这篇关于xml序列化器编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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