TXMLDocument对XML文件进行读取和写入 (bcb)
动态创建TXMLDocument对XML文件进行读取和写入 - [Delphi高级应用]
2008-01-16
Tag:Delphi XML XMLDocument
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://overblue.blogbus.com/logs/13954596.html
uses
XMLDoc, XMLIntf;
{ 写入XML内容 }
var
XMLDoc : TXMLDocument;
Node1 : IXMLNode;
Node2 : IXMLNode;
begin
XMLDoc := TXMLDocument.Create(nil);
try
XMLDoc.Active := True;
XMLDoc.Version := '1.0';
XMLDoc.Encoding := 'GB2312';
XMLDoc.Options := [doNodeAutoCreate,doNodeAutoIndent,doAttrNull,doAutoPrefix,doNamespaceDecl];
XMLDoc.DocumentElement := XMLDoc.CreateNode('ReportObjectContent');
Node1 := XMLDoc.DocumentElement;
Node1 := Node1.AddChild('ReportObjectProperty');
Node2 := Node1.AddChild('ReportName');
Node2.SetAttributeNS('Value', '', ReportName);
Node2 := Node1.AddChild('ReportType');
Node2.SetAttributeNS('Value', '', ReportType);
Node2 := Node1.AddChild('DataViewName');
Node2.SetAttributeNS('Value', '', DataViewName);
Node2 := Node1.AddChild('SQLStr');
Node2.SetAttributeNS('Value', '', SQLStr);
XMLDoc.SaveToStream(Stream);
finally
XMLDoc.Free;
end;
end;
{ 读取XML }
var
XML : TXMLDocument;
Node1 : IXMLNode;
DocIntf : IXMLDocument;
begin
XML := TXMLDocument.Create(nil);
DocIntf := XML; //防止接口被自动释放,少了这一句会发生AV
try
XML.LoadfromStream(Strem);
XML.Active := True;
{ 读ReportObject属性 }
Node1 := XML.DocumentElement.ChildNodes.FindNode('ReportObjectProperty');
Repor
相关文档:
public sealed class XmlHelper
{
public static void Serialize<T>(T obj,string fileName)
{
TextWriter writer = new StreamWriter(fileName);
try
{
XmlSerializer ser = new XmlSerializer(typeof(T));
......
原文:使用 MSXML 分析器处理 XML 文档
#include <atlbase.h>
#include <iostream>
using namespace std;
//<?xml version="1.0"?>
//<xmldata>
//<xmlnode />
//<xmltext>Hello, World!</xmltext>
//</xmldata>
void main ......
已知有一个XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
&nb ......
ref : http://blog.csdn.net/High_Mount/archive/2008/09/19/2953335.aspx
转义字符
不合法的XML字符必须被替换为相应的实体。
如果在XML文档中使用类似"<" 的字符, 那么解析器将会出现错误,因为解析器会认为这是一个新元素的开始。所以不应该象下面那样书写代码:
<message>if salary < 1000 then</me ......