Unity上使用Linq To XML
?
http://blog.csdn.net/lyq5655779/article/details/7183350
1.寫XML文件
[html]?view plaincopy
2.讀取XML文件
? ----------如何讀取xml文件的內(nèi)容----------------
??????????? using(Stream stream=File.OpenRead(@"D:\1.xml"))
??????????? {
?????????????? using(StreamReader reader= new StreamReader(stream))
?????????????? {
?????????????????? //從Reflector從可以看到TextReader的子類是StreamReader所以,可以直接用StreamReader來讀取XML文檔,傳給XDocument.Load方法
????????????????? XDocument xml=XDocument.Load(reader);//load里面
????????????????? Console.WriteLine( xml.Root.ToString());//xml文檔
????????????????? Console.WriteLine(xml.Root.Nodes().ElementAt(0).ToString());//ElementAt()第幾個子節(jié)點?
????????????????? Console.WriteLine(xml.Root.Attribute("age").Value);//返回根節(jié)點的屬性的值
????????????????? XNode nodes=xml.Root.Nodes().ElementAt(0);
????????????????? XElement e=nodes as XElement;
????????????????? Console.WriteLine(e.Attribute("sex").Value);//讀取第一個子節(jié)點的屬性
??????????????? }
????????????
??????????? }
3.讀取App.Config
?
? using (Stream stream = File.OpenRead(@"D:\net實例教程\練習net\XML練習\XML練習\App.config"))
??????????? {
??????????????? using (StreamReader reader = new StreamReader(stream))
??????????????? {
??????????????????? XDocument xml = XDocument.Load(reader);
??????????????????? XNode n1 = xml.Root.Nodes().ElementAt(0);//這就是<configuration> ------下面----<connectionStrings>
??????????????????? XElement e1 = n1 as XElement;
??????????????????? XNode n1_1 = e1.Nodes().ElementAt(0);//connectionStrings下面的<add .....>第一個
??????????????????? XNode n1_2 = e1.Nodes().ElementAt(1);//connectionStrings下面的<add .....>第一個
??????????????????? XElement e1_1 = n1_1 as XElement;
??????????????????? XElement e1_2 = n1_2 as XElement;
??????????????????? //Console.WriteLine("Name={0},Connection={1}", e1.Attribute("name").Value, e1.Attribute("connectionString").Value);
??????????????????? Console.WriteLine("第一個連接字符串的name={0},Connection={1}", e1_1.Attribute("name"), e1_1.Attribute("connectionString"));
??????????????????? Console.WriteLine("第一個連接字符串的name={0},Connection={1}", e1_2.Attribute("name"), e1_2.Attribute("connectionString"));
??????????????? }
??????????? }
???????
?????????
http://blog.csdn.net/xutao_ustc/article/details/6316933
<?xml version="1.0" encoding="utf-8" ?> <UIConfig Count="1"><workflow name="OilInbound"><activity name="Started"><Input><Page></Page><Method></Method></Input><Brief><Page>OilInboundTaskDetail</Page><Method>GetTaskDetailModel</Method> </Brief><Detail><Page>OilInboundTaskDetail</Page><Method>GetTaskDetailModel</Method></Detail><DisplayName>任務創(chuàng)建</DisplayName></activity><activity name="OilCompositionAnalysis"><Input><Page>OilAnalyzingDataInput</Page><Method>GetOilAnalyzingDataInputModel</Method></Input><Brief> <Page>OilAnalyzingDataBrief</Page><Method>GetOilAnalyzingDataBriefModel</Method></Brief><Detail><Page>OilAnalyzingDataDetail</Page><Method>GetOilAnalyzingDataDetailModel</Method></Detail><DisplayName>化驗</DisplayName> </activity></workflow> </UIConfig> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Xml.Linq; using System.Xml; namespace ConsoleApplication2 {class Program{static void Main(string[] args){XElement doc = XElement.Load("..//..//data-config.xml");//根元素string sss = getStr(doc, "OilInbound", "Started", "Input", "Page");Console.WriteLine(sss);Console.ReadKey();}//如果當前元素的子元素只有一個,可以直接用.Element("elementName")來得到,如果是多個子元素就用Elements("elementName")得到private static string getStr(XElement doc,string workflowName,string stepName,string typeName,string pageMethod) {var strEle = from workflow in doc.Elements("workflow")where (string)workflow.Attribute("name") == workflowNameselectnew{str = workflow.Elements("activity").TakeWhile(x => (string)x.Attribute("name") == stepName).First().Element(typeName).Element(pageMethod).Value};return strEle.First().str; }} }?
?
總結(jié)
以上是生活随笔為你收集整理的Unity上使用Linq To XML的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx log error:clie
- 下一篇: mtr命令详解