最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net XMLDocument获取第一个节点与删除root节点方法
时间:2011-05-06 编辑:简简单单 来源:一聚教程网
xml技术来存储数据和文档是一件很容易的事情,.net framework 在它的命名空间system.xml 就提供了一种可以很方便的操作xml的类xmldocument,它使用起来非常容易,xmldocument 其实就是一个简单的树,下面我们就来看看关于 xmldocument获取第一个节点与删除root节点方法
#include "stdafx.h"
using namespace system;
using namespace system::xml;
void navigate(xmlnode ^node, int depth)
{
if (node == nullptr)
return;console::writeline(depth);
console::writeline(node->nodetype.tostring());
console::writeline(node->name);
console::writeline(node->value);if (node->attributes != nullptr)
{
for (int i = 0; i < node->attributes->count; i++)
{
console::writeline(depth+1);
console::writeline(node->attributes[i]->name);
console::writeline(node->attributes[i]->value);
}
}
navigate(node->firstchild, depth+1);
navigate(node->nextsibling, depth);
}
void main()
{
xmldocument ^doc = gcnew xmldocument();
try
{
xmlreader ^reader = xmlreader::create("..monsters.xml");
doc->load(reader);
reader->close();
xmlnode ^node = doc->firstchild;
navigate(node, 0);
}
catch (exception ^e)
{
console::writeline("error occurred: {0}", e->message);
}
}
删除root节点
#include "stdafx.h"
using namespace system;
using namespace system::xml;
using namespace system::xml::xpath;void navigate(xpathnavigator ^nav, int depth)
{
console::writeline(depth);
console::writeline(nav->nodetype.tostring());
console::writeline(nav->name);
console::writeline(nav->value);if (nav->hasattributes)
{
nav->movetofirstattribute();
do {
console::writeline(nav->name);
console::writeline(nav->value);
}
while(nav->movetonextattribute());
nav->movetoparent();
}
if (nav->movetofirstchild())
{
navigate(nav, depth+1);
nav->movetoparent();
}
if (nav->movetonext())
navigate(nav, depth);
}void main()
{
xmldocument ^doc = gcnew xmldocument();
try
{
doc->load("..monsters.xml");
xpathnavigator ^nav = doc->createnavigator();
nav->movetoroot();
navigate(nav, 0);
}
catch (exception ^e)
{
console::writeline("error occurred: {0}", e->message);
}
}
-
上一个: asp.net C++输出 xml代码
-
下一个: C# 命名空间入门级用法
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10