程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#操作xml文件示例

c#操作xml文件示例

編輯:C#入門知識

c#操作xml文件示例。本站提示廣大學習愛好者:(c#操作xml文件示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#操作xml文件示例正文


1. 新增XML文件


XMLToolV2 _xmlHelper = new XMLToolV2(@"C:\20140311blogs.xml");//xml保留途徑或許讀取途徑
 _xmlHelper.Create("Person", "utf-8");//跟節點稱號:person;encode:utf-8
 XmlElement _person = _xmlHelper.CreateElec("Name", "Yan-Zhiwei");//在跟節點後創立person節點
 _xmlHelper.SetAttribute(_person, "Gender", "Man");//設置person節點屬性Gender
 _xmlHelper.SetAttribute(_person, "Address", "shanghai");//設置person節點屬性Address
 _xmlHelper.Save();//保留xml文件

上述代碼完成後果:

那在Person節點持續增長節點,完成也很簡略


_xmlHelper.Create("Person", "utf-8");//跟節點稱號:person;encode:utf-8
XmlElement _person = _xmlHelper.CreateElec("Name", "Yan-Zhiwei");//在跟節點後創立person節點
_xmlHelper.SetAttribute(_person, "Gender", "Man");//設置person節點屬性Gender
_xmlHelper.SetAttribute(_person, "Address", "shanghai");//設置person節點屬性Address
XmlElement _workLh = _xmlHelper.CreateElec(_person, "Work", "shanghai LH");//在person節點下增長work節點
_xmlHelper.SetAttribute(_workLh, "Year", "2013~");//設置work節點屬性Title
XmlElement _workRK = _xmlHelper.CreateElec(_person, "Work", "shanghai Ranking");//在person節點下增長work節點
_xmlHelper.SetAttribute(_workRK, "Year", "2011~2013");//設置work節點屬性Title
_xmlHelper.Save();//保留xml文件

上述代碼完成後果:

2.讀取節點的值,小我感到xpath方法比linq to xml更加清楚明了便利


XMLToolV2 _xmlHelper = new XMLToolV2(@"C:\20140311blogs.xml");//xml保留途徑或許讀取途徑
XmlNode _person = _xmlHelper.Select("Person/Name/text()");
Console.WriteLine("Name:" + _person.InnerText);
XmlNode _gender = _xmlHelper.Select("Person/Name[@Gender='Man']");
Console.WriteLine("Gender:" + _gender.Attributes["Gender"].Value);
Console.WriteLine("Address:" + _gender.Attributes["Address"].Value);
Console.WriteLine("-----------------------------");
XmlNodeList _workRecord = _xmlHelper.SelectNodeList("Person/Name/Work");
XMLToolV2.Loop(_workRecord, (XmlNode node) =>
{
    Console.WriteLine("Work:" + node.InnerText + " Year:" + node.Attributes["Year"].Value);
});


上述代碼完成後果:

3.格局化顯示XML


XMLToolV2 _xmlHelper = new XMLToolV2(@"C:\20140311blogs.xml");//xml保留途徑或許讀取途徑
string _xmlString = _xmlHelper.ShowXml();
Console.WriteLine(XMLToolV2.FormatXml(_xmlString, "utf-8"));


上述代碼完成後果:

4.XMLToolV2源代碼


public class XMLToolV2
    {
        static string _xmlPath;
        static XmlDocument _xmlDoc { get; set; }
        static XmlElement _xmlRoot;


        public XMLToolV2(string xmlPath)
        {
            _xmlPath = xmlPath;
            LoadXmlDoc();
        }
        public XmlNode Select(string xPath)
        {
            if (string.IsNullOrEmpty(xPath))
                throw new ArgumentNullException("xPath");
            return _xmlDoc.SelectSingleNode(xPath);
        }
        public XmlNodeList SelectNodeList(string xPath)
        {
            if (string.IsNullOrEmpty(xPath))
                throw new ArgumentNullException("xPath");
            return _xmlDoc.SelectNodes(xPath);
        }
        public void Create(string rootName, string encode)
        {
            CreateXmlDoc(rootName, encode);
        }
        private void CreateXmlDoc(string rootName, string encode)
        {
            if (!File.Exists(_xmlPath))
            {
                if (string.IsNullOrEmpty(rootName))
                    throw new ArgumentNullException(rootName);
                _xmlDoc = new XmlDocument();
                XmlDeclaration _xmldecl = _xmlDoc.CreateXmlDeclaration("1.0", encode, null);
                _xmlDoc.AppendChild(_xmldecl);
                _xmlRoot = _xmlDoc.CreateElement("", rootName, "");
                _xmlDoc.AppendChild(_xmlRoot);
            }
        }
        public void LoadXmlDoc()
        {
            if (File.Exists(_xmlPath))
            {
                _xmlDoc = new XmlDocument();
                _xmlDoc.Load(_xmlPath);
                _xmlRoot = _xmlDoc.DocumentElement;
            }
        }
        public void Save()
        {
            if (_xmlDoc != null)
            {
                _xmlDoc.Save(_xmlPath);
            }
        }
        public XmlElement CreateElec(string elecName, string elecValue)
        {
            XmlElement _xElec = CreateElement(_xmlRoot, elecName, elecValue);
            return _xElec;
        }

        private XmlElement CreateElement(XmlNode _xmldocSelect, string elecName, string elecValue)
        {
            if (_xmldocSelect != null)
            {
                XmlElement _xElec = _xmlDoc.CreateElement(elecName);
                _xElec.InnerText = elecValue;
                _xmldocSelect.AppendChild(_xElec);
                return _xElec;
            }
            return null;
        }

        public XmlElement CreateElec(XmlElement xmlParentElec, string elecName, string elecValue)
        {
            if (xmlParentElec != null)
            {
                XmlElement _xElec = CreateElement(xmlParentElec, elecName, elecValue);
                return _xElec;
            }
            return null;
        }
        public void SetAttribute(XmlElement xElement, string attrName, string attrValue)
        {
            if (xElement != null)
            {
                xElement.SetAttribute(attrName, attrValue);
            }
        }
        public int Check(string xpath)
        {
            if (string.IsNullOrEmpty(xpath))
                throw new ArgumentNullException("xpath");
            return SelectNodeList(xpath).Count;
        }
        public string ShowXml()
        {
            if (_xmlDoc != null)
            {
                return _xmlDoc.OuterXml;
            }
            return string.Empty;
        }
        public static string FormatXml(string xmlString, string encode)
        {
            if (string.IsNullOrEmpty(xmlString))
                throw new ArgumentNullException("xmlString");
            if (string.IsNullOrEmpty(encode))
                throw new ArgumentNullException("encode");
            MemoryStream _mstream = new MemoryStream();
            XmlTextWriter _writer = new XmlTextWriter(_mstream, null);
            XmlDocument _xDoc = new XmlDocument();
            _writer.Formatting = Formatting.Indented;

            _xDoc.LoadXml(xmlString);
            _xDoc.WriteTo(_writer);
            _writer.Flush();
            _writer.Close();

            Encoding _encoding = Encoding.GetEncoding(encode);
            string _xmlString = _encoding.GetString(_mstream.ToArray());
            _mstream.Close();
            return _xmlString;
        }
        public static void Loop(XmlNodeList nodeList, Action<XmlNode> xmlNode)
        {
            if (nodeList != null)
            {
                foreach (XmlNode xNode in nodeList)
                {
                    xmlNode(xNode);
                }
            }
        }
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved