当前位置:编程学习 > XML/UML >>

C#对XML的增删改查操作

好久不用XML了。最近做Silverlight项目,需要通过Web Service访问一些C++的Dll.
使用XML传递数据。正好复习一下XML操作。

\大气象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Xml;
using System.IO;

namespace HCLoad.Web
{
    public partial class TestXml : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadXml();
        }
        private void LoadXml()
        {
            XmlDocument xmlDoc = new XmlDocument();
            string strXml =
            @"<?xml version=""1.0"" encoding=""utf-16"" ?>
            <projects>
                <project id=""1"">p1</project>
                <project id=""2"">
                    <name>p2</name>
                </project>
            </projects>";
            xmlDoc.LoadXml(strXml);

            //Response.Write("<script>alert(" + xmlDoc.OuterXml + ");</script>");//OuterXml是该结点包含的全部内容
            //Response.Write(xmlDoc.OuterXml);//直接在浏览器中输出xml文档是空白,因为浏览器无法解析这些标签。

            //根据属性值查询
            XmlElement theProject = (XmlElement)xmlDoc.DocumentElement.SelectSingleNode("/projects/project[@id=1]");
            Response.Write(theProject.InnerText);

            //根据子节点值查询
            XmlElement theProject2 = (XmlElement)xmlDoc.DocumentElement.SelectSingleNode("/projects/project[name=p2]");
            Response.Write(theProject2.InnerText);//注意是查询到name那一层了。

            //通过标签名查询,并修改
            xmlDoc.GetElementsByTagName("project").Item(0).InnerText = 
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,