C#显示XML元素内容的简单例子
接上一篇
显示所有结点的内容
1 原xml文件 bookstore.xml
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="1234123">
<title>who am i </title>
<author>who</author>
<price>999</price>
</book>
<book>
</book>
</bookstore>
2 program.cs
using System;
using System.Xml;
namespace ReadXml
{
class Class1
{
static void Main(string[] args)
{
//实例化一个XmlDocument对象
XmlDocument xmlDoc = new XmlDocument();
//实例对象读取要写入的XML文件
xmlDoc.Load("bookstore.xml");
XmlNode xn = xmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
//该属性没有,不显示,但不会报错
Console.WriteLine(xe.GetAttribute("genre"));
//显示属性值
&nb
相关文档:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Data;
public class Cls_XML
{
#region 创建xml文件
/// <summary>
/// 创建xml文件
/// ......
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot ;
// 创建DOMDocument对象
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if ( ! SUCCEEDED(hr))
{
MessageBox( " 无法创建DOMDocument对象,请检查 ......
XML 序列化可以采用从简单到复杂的多种形式。例如,可以序列化只包含公共字段和公共属性的类,如 XML 序列化简介中所示。下面的代码示例讨论各种高级方案,包括如何使用 XML 序列化生成符合特定 XML 架构 (XSD) 文档的 XML 流。
序列化数据集
除了序列化公共类的实例外,还可序列化 DataSet 的实例,如下面的代码示例所示 ......
接上一篇《C#写XML的简单例子》
这个例子要修改XML文件中结点的属性和和元素的文本
1 原xml文件 bookstore.xml
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="love" ISBN="1234123">
<title>who am i </title>
&l ......