包含xmldocument的词条
c# 怎么使用XmlDocument
---给你几个函数吧
using System.Xml;
public void WriteXml(string filename)
{
//生成ProgramList.xml
XmlDocument ProgramListXmlDoc = new XmlDocument();
XmlDeclaration xmldecl = ProgramListXmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
ProgramListXmlDoc.AppendChild(xmldecl);
//加入一个根元素
XmlElement xmlelem = ProgramListXmlDoc.CreateElement("", "root", "");
XmlNode root = ProgramListXmlDoc.AppendChild(xmlelem);
xmlelem = ProgramListXmlDoc.CreateElement("sysoption");
XmlElement tmpelem = ProgramListXmlDoc.CreateElement("sc_server_ip");
tmpelem.InnerText = Sc_server_ip;
xmlelem.AppendChild(tmpelem);
tmpelem = ProgramListXmlDoc.CreateElement("device_type");
tmpelem.InnerText = Device_type;
xmlelem.AppendChild(tmpelem);
tmpelem = ProgramListXmlDoc.CreateElement("station_id");
tmpelem.InnerText = Station_id;
xmlelem.AppendChild(tmpelem);
tmpelem = ProgramListXmlDoc.CreateElement("serial_number");
tmpelem.InnerText = Serial_number;
xmlelem.AppendChild(tmpelem);
tmpelem = ProgramListXmlDoc.CreateElement("database_ip");
tmpelem.InnerText = database_ip;
xmlelem.AppendChild(tmpelem);
root.AppendChild(xmlelem);
ProgramListXmlDoc.Save(filename);
}
public void LoadXml(string filename)
{
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(filename);
Sc_server_ip = xmldoc.SelectSingleNode("root/sysoption/sc_server_ip").InnerText.Trim();
Serial_number = xmldoc.SelectSingleNode("root/sysoption/serial_number").InnerText.Trim();
sc_server_ip = xmldoc.SelectSingleNode("root/sysoption/sc_server_ip").InnerText.Trim();
database_ip = xmldoc.SelectSingleNode("root/sysoption/database_ip").InnerText.Trim();
}
catch (Exception e)
{
}
}
XmlDocument是什么类
是读取以及处理Xml文本的类
比如你有一个Xml文本
Books
Book
NameA/Name
/Book
/Books
那么就可以
XmlDocument doc = new XmlDocument()
cos.Load("....");
这样来读取
然后进行处理,遍历等等
c#的xmldocument能不能用在html?
如果html的头部有类似?xml version="1.0" encoding="utf-8"?的定义,而且整个文档中的所有标签都是闭合的,可以使用xmldocument,xml本身并不对文档中定义什么样的标签有限制,只要闭合就ok,可以说html的是xml的一种应用
如何用XMLDocument解析XML,得到节点信息
参考如下代码:
procedure TForm1.btn1Click(Sender: TObject);
function SetXMLFormatStr(const AStr: string): string;
begin
Result := '';
Result := '' + AStr + '';
end;
var
tmpFilePath: string;
tmpfilestream: TFileStream;
tmpFileMemoryStream: TMemoryStream;
buffer: array of char;
p: PChar;
pFlag, p0: PChar;
tmpOffset: longword;
tmpStr, tmpFieldNameStr, tmpvalue: string;
tmpValueStr: array of string;
tmpFileSize: Cardinal;
tmpLen: Cardinal;
I,J: Integer;
begin
opendlg.Filter := 'xml文件|*.XML';
if opendlg.Execute then
begin
tmpFilePath := opendlg.FileName;
end;
Edit1.Text := tmpFilePath;
tmpfilestream := TFileStream.Create(tmpFilePath, fmOpenRead or fmShareExclusive);
try
//获得filestream的大小
//指针指向该stream,
//按照格式循环取对应值;
//赋值给array of string 数组;
tmpfilestream.Position := 0;
tmpFileSize := tmpFileStream.Size;
SetLength(buffer, tmpFileSize);
tmpFileStream.Read(buffer[0], tmpFileSize);
finally
FreeAndNil(tmpfilestream);
end;
mmo1.Lines.Add(PChar(buffer));
try
p := PChar(buffer);
p0 := PChar(buffer);
while (p - p0) tmpFileSize do
begin
while p^ in [#0..#32] do
p := CharNext(p);
//读文件头
//读root部分
//读item部分 //每个item之间存着数字部分 (循环读入)
//进入实际的数值部分,进行""的提取;
//读root部分
//读item部分
//读文件尾
if p^ = '' then //如果是item ,那么就要取之间的内容;
begin
pFlag := p; //记住这个位置,找对应的
while p^ '' do
p := charnext(p);
if p^ = '' then
begin
//获得 filedname;
SetString(tmpFieldNameStr, pFlag,p - pFlag + 1);
mmo2.lines.add(tmpFieldNameStr);
for I := 0 to Length(cNodeFileNameArr) -1 do
begin
if tmpFieldNameStr = SetXMLFormatStr(cNodeFileNameArr[I]) then
begin
p := charnext(p);
pFlag := p;
while p^ '' do
p := charnext(p);
if p^ = '' then
begin
SetString(tmpvalue, pFlag,p - pFlag);
mmo2.lines.add(tmpvalue); //打印值;
end;
Break;
end;
end;
end;
p := charnext(p);
end;
p := charnext(p);
end;
finally
FreeAndNil(tmpfilestream);
end;
end;