using System;
using System.Xml;namespace prjCx
{
    /// <summary>
    /// Config 的摘要说明。
    /// </summary>
    public class Config
    {
        private String msFileName = null;        public String ConfigFile
        {
            get
            {
                return this.msFileName;
            }
            set
            {
                if (System.IO.File.Exists(value.Trim()))
                {
                    this.msFileName = value.Trim();
                }
            }
        }        public Config()
        {
            this.msFileName = String.Empty;
        }        public Config(String ConfigFile)
        {
            this.ConfigFile = ConfigFile.Trim();
        }        public bool ReadConfig(String ContentName, out String ContentValue)
        {
            bool bFlag = false;            ContentValue = String.Empty;            if (!System.IO.File.Exists(this.msFileName))
            {
                return bFlag;
            }            try
            {
                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                xmlDoc.Load(this.msFileName);
                System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
                ContentValue = xmlNode.InnerText;                bFlag = true;
            }
            catch (XmlException xmle)
            {
                System.Console.WriteLine(xmle.Message);
            }            return bFlag;
        }
        
        /// <summary>
        /// 读XML文件
        /// </summary>
        /// <param name="ContentName">节点名</param>
        /// <param name="PropertyName">属性名</param>
        /// <param name="PropertyValue">属性值(Ref)</param>
        /// <returns></returns>
        public bool ReadConfig(String ContentName, String PropertyName, out String PropertyValue)
        {
            bool bFlag = false;            PropertyValue = String.Empty;            if (!System.IO.File.Exists(this.msFileName))
            {
                return bFlag;
            }            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(this.msFileName);                XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);                XmlAttributeCollection xmlAttr = xmlNode.Attributes;                for (int i = 0; i < xmlAttr.Count; ++i)
                {
                    if (xmlAttr.Item(i).Name == PropertyName)
                    {
                        PropertyValue = xmlAttr.Item(i).Value;
                        bFlag = true;
                        break;
                    }
                }
            }
            catch (XmlException xmle)
            {
                System.Console.WriteLine(xmle.Message);
            }            return bFlag;
        }        public bool WriteConfig(String ContentName, String ContentValue)
        {
            bool bFlag = false;            if (!System.IO.File.Exists(this.msFileName))
            {
                return bFlag;
            }            try
            {
                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                xmlDoc.Load(this.msFileName);
                System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
                xmlNode.InnerText = ContentValue;                xmlDoc.Save(this.msFileName);                bFlag = true;
            }
            catch (XmlException xmle)
            {
                System.Console.WriteLine(xmle.Message);
            }            return bFlag;
        }
        /// <summary>
        /// 写XML文件
        /// </summary>
        /// <param name="ContentName">节点名</param>
        /// <param name="PropertyName">属性名</param>
        /// <param name="PropertyValue">属性值</param>
        /// <returns></returns>
        public bool WriteConfig(String ContentName, String PropertyName, String PropertyValue)
        {
            bool bFlag = false;            if (!System.IO.File.Exists(this.msFileName))
            {
                return bFlag;
            }            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(this.msFileName);                XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);                XmlAttributeCollection xmlAttr = xmlNode.Attributes;                for (int i = 0; i < xmlAttr.Count; ++i)
                {
                    if (xmlAttr.Item(i).Name == PropertyName)
                    {
                        xmlAttr.Item(i).Value = PropertyValue;
                        bFlag = true;
                        break;
                    }
                }                xmlDoc.Save(this.msFileName);                bFlag = true;
            }
            catch (XmlException xmle)
            {
                System.Console.WriteLine(xmle.Message);
            }            return bFlag;
        }
    }
}

解决方案 »

  1.   

    建一下控制台程序,将以下代码拷进去,试一下:
    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    string strXML="<?xml version='1.0' encoding='gb2312' ?><Root>";
    strXML+="<ItemNode 姓名='胡军' 批号='1' ></ItemNode>";
    strXML+="<ItemNode 姓名='张三' 批号='2' ></ItemNode></Root>";
    XmlDocument xmldoc=new XmlDocument();
    xmldoc.LoadXml(strXML);
    XmlElement xe=xmldoc.DocumentElement;
    for(int i=0;i<xe.ChildNodes.Count;i++)
    {
    XmlNode xn=xe.ChildNodes[i];
    Console.Write("第{0}条记录 ",i+1);
    for(int j=0;j<xn.Attributes.Count;j++)
    {
    Console.Write("{0}={1} ",xn.Attributes[j].Name,xn.Attributes[j].Value);
    }
    Console.Write("\n");
    }
    Console.ReadLine();}
    }
      

  2.   

    给你一个XML操作类和一个XPATH 的网址参考,啥也不说了.一切尽在不言中
    http://www.webdn.com/web_file/3sword/xml/0602016042/
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Xml;
    using System.Data;
    namespace Solo_Spirit
    {
        public class XmlParser
        {
            //XML解析类,param为xpath
            private XmlDocument doc;
            private string configFile;
            public XmlParser(string fileName)
            {
                doc = new XmlDocument();
                configFile = fileName;
                try
                {
                    doc.Load(configFile);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            public string ConfigFile
            {
                get { return configFile; }
                set { configFile = value; }
            }
            /// <summary>
            /// 读取指定结点信息
            /// </summary>
            /// <param name="sectionName">父节点</param>
            /// <param name="nodename">子节点名称</param>
            /// <returns></returns>
            public string GetAppsetting(string sectionName, string nodename)
            {
                string result = string.Empty;
                XmlNodeList nodelist = null;
                try
                {
                    nodelist = doc.SelectSingleNode(sectionName).ChildNodes;
                }
                catch
                {
                    result = "Cannot find the value of" + sectionName + "in ConfigFile!";
                }
                foreach (XmlNode node in nodelist)
                {
                    if (node.Name == nodename)
                    {
                        result = node.InnerText;
                        break;
                    }
                    else
                    {
                        result = "Cannot find the value of" + nodename + "in ConfigFile!";
                    }
                }
                return result;
            }
            /// <summary>
            /// 返回指定结点的值
            /// </summary>
            /// <param name="XmlPathNode">路径(xpath)</param>
            /// <returns></returns>
            public string GetNodeValue(string XmlPathNode)
            {
                string nodeValue;
               nodeValue=doc.SelectSingleNode(XmlPathNode).InnerText;
               return nodeValue;
            }
            /// <summary>
            /// 查找指定节点的数据 返回DataView
            /// </summary>
            /// <param name="XmlPathNode"></param>
            /// <returns>DataView</returns>
            public DataView GetDataView(string XmlPathNode)
            {
                DataSet dst = new DataSet();
                StringReader read = new StringReader(doc.SelectSingleNode(XmlPathNode).OuterXml);
                dst.ReadXml(read);
                return dst.Tables[0].DefaultView;
            }
            public DataTable GetDataTable(string XmlPathNode)
            {
                DataSet dst = new DataSet();
                StringReader read = new StringReader(doc.SelectSingleNode(XmlPathNode).OuterXml);
                dst.ReadXml(read);
                return dst.Tables[0];
            }
            /// <summary>
            /// 更新指定节点的内容
            /// </summary>
            /// <param name="XmlPathNode">节点</param>
            /// <param name="Content">内容</param>
            public void EditNode(string XmlPathNode, string Content)
            {
                doc.SelectSingleNode(XmlPathNode).InnerText = Content;
            }        /// <summary>
            /// 删除指定节点
            /// </summary>
            /// <param name="Node">节点</param>
            public void Delete(string Node)
            {
                string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
                doc.SelectSingleNode(mainNode).RemoveChild(doc.SelectSingleNode(Node));
            }        /// <summary>
            /// 插入一个节点和此节点的一子节点。
            /// </summary>
            /// <param name="MainNode">根节点</param>
            /// <param name="ChildNode">子节点</param>
            /// <param name="Element">元素名</param>
            /// <param name="Content">值</param>
            public void InsertNode(string MainNode, string ChildNode, string Element, string Content)
            {
                XmlNode objRootNode = doc.SelectSingleNode(MainNode);
                XmlElement objChildNode = doc.CreateElement(ChildNode);
                objRootNode.AppendChild(objChildNode);
                XmlElement objElement = doc.CreateElement(Element);
                objElement.InnerText = Content;
                objChildNode.AppendChild(objElement);
            }
            /// <summary>
            /// 插入一节点 包括一个属性
            /// </summary>
            /// <param name="MainNode">根节点</param>
            /// <param name="Element">子节点</param>
            /// <param name="Attrib">属性名</param>
            /// <param name="AttribContent">属性内容</param>
            /// <param name="Content">子节点内容</param>
            public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content)
            {
                XmlNode objNode = doc.SelectSingleNode(MainNode);
                XmlElement objElement = doc.CreateElement(Element);
                objElement.SetAttribute(Attrib, AttribContent);
                objElement.InnerText = Content;
                objNode.AppendChild(objElement);
            }        /**/
            /// <summary>
            /// 插入一节点不带属性
            /// </summary>
            /// <param name="MainNode"></param>
            /// <param name="Element"></param>
            /// <param name="Content"></param>
            public void InsertElement(string MainNode, string Element, string Content)
            {
                XmlNode objNode = doc.SelectSingleNode(MainNode);
                XmlElement objElement = doc.CreateElement(Element);
                objElement.InnerText = Content;
                objNode.AppendChild(objElement);
            }
            /// <summary>
            /// 保存文档
            /// </summary>
            public void Save()
            {
                try
                {
                    doc.Save(configFile);
                }
                catch (System.Exception ex)
                {
                    throw new System.Exception(ex.ToString());
                }
            }
            public void dispose()
            {
                doc = null;
            }    }
    }
      

  3.   

    啥也别说了,看看我的BOLG中的一篇文章,最适合初学者
    http://blog.csdn.net/yumanqing/archive/2007/03/20/1534744.aspx
      

  4.   

    TO:那怎么获得列名呢?
    string  strXML="?xml  version='1.0'  encoding='gb2312'  ?><Root>";  
                 strXML+="<ItemNode  姓名='胡军'  批号='1'  ></ItemNode>";  
                 strXML+="<ItemNode  姓名='张三'  批号='2'  ></itemNode></Root>";  如我要获得,姓名这个列名,我先只能获得值,不能获得列名~~~for example:string strXML = "<?xml version='1.0' encoding='gb2312' ?><Root>";
                strXML += "<ItemNode 姓名='胡军' 批号='1' ></ItemNode>";
                strXML += "<ItemNode 姓名='张三' 批号='2' ></ItemNode></Root>";
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(strXML);
                XmlNodeList nodes = doc.SelectNodes(@"//ItemNode");
                foreach (XmlNode node in nodes)
                {
                    foreach (XmlAttribute attribute in node.Attributes)
                    {
                        Console.Write(attribute.Name + "=" + attribute.Value + "\t");
                    }
                    Console.WriteLine();
                }输出:
    姓名=胡军批号=1
    姓名=张三批号=2
      

  5.   

    用Xml的Attribute一定可以的啊
      

  6.   

    你所说的列名,实际上就是xml结点属性名,直接XmlAttribute.Name,遍历当前结点的所有属性,得到属性的Name和Value...具体代码如上所示....