读写XML文件 [英] Reading and writing into the XML file

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

问题描述

我需要使用Delphi处理XML文件。

I need to work on XML files using Delphi.

我想将DBGrid中的xml数据呈现给用户,并将用户所做的更改保存在XML文件中。

I want to present the xml data in a DBGrid to the user and save the changes done by user in the XML file.

例如,在下面的xml中(向用户显示),如果用户更改了客户端下的ABC市添加了昵称为 AAA的新客户这些更改应反映在XML文件中。

For example in the below xml (which was presented to the user), if user changed City of ABC under client and added a new customer with NickName as "AAA" those changes should be reflected in the XML file.

<Data LinkID="0">
  <Client>
      <Item Name="ABC" Mobile="1234" City="IN" />
      <Item Name="PQR" Mobile="5678" City="IN" />
  </Client>
  <Customer>
    <Item NickName="XYZ" Phone="1254" City="IN" />
    <Item NickName="MNO" Phone ="41255" City="IN" />
  </Customer>
</Data>

我正在使用XMLDocument和ClientDataSet来实现此目标,但没有成功。

I am working with XMLDocument and ClientDataSet to achieve this but without success.

有人可以帮助我实现这一目标吗?

Can anyone help me in achieving this?

推荐答案

问题

XML文件应采用指定的格式,且必须具有< METADATA>和< ROWDATA> 标签。

The XML file should be in a specified format which must have <METADATA> and <ROWDATA> tags.

我将xml更改为该格式。

I changed the xml to that format.

<?xml version="1.0" standalone="yes"?>  
<DATAPACKET Version="2.0">
<METADATA>
<FIELDS>
<FIELD attrname="Name" fieldtype="string" WIDTH="50"/>
<FIELD attrname="Mobile" fieldtype="string" WIDTH="20"/>
<FIELD attrname="City" fieldtype="string" WIDTH="20"/>
</FIELDS><PARAMS CHANGE_LOG="6 1 8"/>
</METADATA>
<ROWDATA>
<ROW Name="ABC" Mobile="1234" City="IN"/>
<ROW Name="PQR" Mobile="5678" City="IN"/>
<ROW Name="AAA" Mobile="7894" City="IN"/>
<ROW Name="MNO" Mobile="4569" City="IN"/>
<ROW Name="ABC" Mobile="45685" City="IN"/>
</ROWDATA>
</DATAPACKET>

接下来我用
ClientDataSet1.LoadFromFile('E:\ \使用CDS\XmlText.xml对项目XML样本应用程序进行操作); 读取XML文件。

Next i used ClientDataSet1.LoadFromFile('E:\projects\XML\Sample App with CDS\XmlText.xml'); to read the XML file.

修改后在网格中完成,我使用了 ClientDataSet1.SaveToFile('E:\projects\XML\Sample App with CDS\XmlText.xml',dfXML); 方法保存回XML。

After modifications were done in the grid i used ClientDataSet1.SaveToFile('E:\projects\XML\Sample App with CDS\XmlText.xml',dfXML); method to save back to the XML.

为此不需要XMLDocument。

There is no need of XMLDocument for this purpose.

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

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