最近在学读取文件
XML (可扩展标记语言,eXtensible Markup Language) 是一种标记语言,它被设计用来传输和存储数据。
第一行是 XML 声明,它定义 XML 的版本和所使用的编码),然后是根元素,然后是根元素的子元素(当然,可以继续嵌套)
XML的结构由标签、元素、属性和文本组成。
标签(tag):以尖括号(< >)包围的单词或词组,用来起始和结束元素。
元素(element):指标签包含的整个内容。元素通常由开始标签和结束标签组成,有时也可以是自封闭的标签。
属性(attribute):定义在开始标签内的附加信息。属性是由名称和值组成的,它们用空格隔开。
文本(text):元素内部不包含标签的部分。文本可以包含实际的数据或者说明信息。
<person>
<person p="man">
<name>LiHuaname>
<age>18age>
<height>172height>
person>
<person p="woman">
<name>Lingname>
<age>18age>
<height>168height>
person>
person>
有了这个XML文件之后,接下来,我们可以对它进行一些操作了!!
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace _02_XML操作
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Person.xml");
XmlNode root = xmlDoc.ChildNodes[1];
XmlNodeList personList = root.ChildNodes;
foreach (XmlNode person in personList)
{
foreach (XmlNode node in person.ChildNodes)
{
Console.WriteLine(node.Name + ":" + node.InnerText);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace _02_XML操作
{
class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(File.ReadAllText("Person.xml"));
XmlNode root = xmlDoc.ChildNodes[1];
XmlNodeList personList = root.ChildNodes;
foreach(XmlNode person in personList)
{
Person obj = new Person();
foreach(XmlNode node in person.ChildNodes)
{
if (node.Name == "name")
{
obj.name = node.InnerText;
}else if (node.Name == "age")
{
obj.age = Int32.Parse(node.InnerText);
}else if (node.Name == "height")
{
obj.height = Int32.Parse(node.InnerText);
}
}
list.Add(obj);
}
foreach(Person p in list)
{
Console.WriteLine(p.name + "," + p.age + "," + p.height);
}
}
}
}
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace _02_XML操作
{
class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(File.ReadAllText("Person.xml"));
XmlNode root = xmlDoc.ChildNodes[1];
XmlNodeList personList = root.ChildNodes;
foreach(XmlNode person in personList)
{
Person obj = new Person();
XmlElement nameEle = person["name"];
obj.name = nameEle.InnerText;
XmlElement ageEle = person["age"];
obj.age = Int32.Parse(ageEle.InnerText);
XmlElement heightEle = person["height"];
obj.height = Int32.Parse(heightEle.InnerText);
list.Add(obj);
}
foreach(Person p in list)
{
Console.WriteLine(p.name + "," + p.age + "," + p.height);
}
}
}
}
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace _02_XML操作
{
class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(File.ReadAllText("Person.xml"));
XmlNode root = xmlDoc.ChildNodes[1];
XmlNodeList personList = root.ChildNodes;
foreach(XmlNode person in personList)
{
Person obj = new Person();
//读取属性
XmlAttributeCollection attriCol = person.Attributes;
XmlAttribute attri = attriCol["p"];
obj.p = attri.Value;
XmlElement nameEle = person["name"];
obj.name = nameEle.InnerText;
XmlElement ageEle = person["age"];
obj.age = Int32.Parse(ageEle.InnerText);
XmlElement heightEle = person["height"];
obj.height = Int32.Parse(heightEle.InnerText);
list.Add(obj);
}
foreach(Person p in list)
{
Console.WriteLine(p.p+","+p.name + "," + p.age + "," + p.height);
}
}
}
}
更多内容请前往主页置顶或点击链接直达:
【unity3D】游戏开发专栏的目录
【VR】VR开发专栏的目录