题记:目前在制作一个工具,需要使用XML文档,因此读取和写入文档是个十分重要的,今天进行了一下探索,在这里记录一下,以作为笔记。
C#读取XML文档
使用XMLDocument对象,可以很方便的读取XML文档,C#中对XML的操作进行的封装,相比较于C++,则要方便很多。
下面是样例XML文档:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!--This is a continent file to contain all continent in our world--> <Root> <Continents> <Continent ID="101" Name="Asia" /> <Continent ID="102" Name="Africa" /> <Continent ID="103" Name="North America" /> <Continent ID="104" Name="South America" /> <Continent ID="105" Name="Europe" /> <Continent ID="106" Name="Oceania" /> <Continent ID="107" Name="Antarctica" /> </Continents> </Root>
读取该文档的样例代码如下:
string strFilePathName = strFilePath + "\\Continent.xml"; List<Continent> data = new List<Continent>(); Console.WriteLine("The data is parsed from file :" + strFilePathName); try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(strFilePathName); XmlNode xmlNode = xmlDoc.SelectSingleNode("Root"); if (xmlNode == null) { Console.WriteLine("Invalid file format!"); return; } XmlNode continentNode = xmlNode.FirstChild; foreach (XmlNode node in continentNode.ChildNodes) { uint uiID = uint.Parse(node.Attributes["ID"].Value); string strName = node.Attributes["Name"].Value; data.Add(new Continent(uiID, strName)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Parsed OK, result:"); foreach (Continent info in data) { Console.WriteLine("ID= " + info.ID + ", Name = " + info.Name); }
C# 写XML文档
C#写XMl文档也比较简单,首先使用XMLDocument对象创建XML文档,然后写入XMLWriter流,进行flush就行了。
下面是样例代码:
string strFilePathName = strFilePath + "\\Continent.xml"; // write // Asia, Africa, North America, South America,Europe, Oceania, Antarctica List<Continent> data = new List<Continent>(); uint uiIDBegin = 101; data.Add(new Continent(uiIDBegin++, "Asia")); data.Add(new Continent(uiIDBegin++, "Africa")); data.Add(new Continent(uiIDBegin++, "North America")); data.Add(new Continent(uiIDBegin++, "South America")); data.Add(new Continent(uiIDBegin++, "Europe")); data.Add(new Continent(uiIDBegin++, "Oceania")); data.Add(new Continent(uiIDBegin++, "Antarctica")); foreach (Continent info in data) { Console.WriteLine("ID= " + info.ID + ", Name = " + info.Name); } Console.WriteLine("The data will write to file :" + strFilePathName); try { XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes"); XmlComment xmlComment = xmlDoc.CreateComment("This is a continent file to contain all continent in our world"); XmlElement xmlRoot = xmlDoc.CreateElement("Root"); XmlElement xmlContinents = xmlDoc.CreateElement("Continents"); xmlRoot.AppendChild(xmlContinents); foreach (Continent info in data) { XmlElement xmlContinent = xmlDoc.CreateElement("Continent"); xmlContinents.AppendChild(xmlContinent); xmlContinent.SetAttribute("ID", info.ID.ToString()); xmlContinent.SetAttribute("Name", info.Name); } xmlDoc.AppendChild(xmlDec); xmlDoc.AppendChild(xmlComment); xmlDoc.AppendChild(xmlRoot); XmlWriterSettings wrSettings = new XmlWriterSettings(); wrSettings.Indent = true; wrSettings.Encoding = UnicodeEncoding.UTF8; XmlWriter xmlWr = XmlWriter.Create(strFilePathName, wrSettings); xmlDoc.WriteTo(xmlWr); xmlWr.Flush(); Console.WriteLine("Create OK, file name = " + strFilePathName); } catch (Exception ex) { Console.WriteLine(ex.Message); }
后记:
这里仅仅是个简单的例子,以备往后复制黏贴之用,呵呵!