日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Unity上使用Linq To XML

發(fā)布時(shí)間:2025/3/21 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity上使用Linq To XML 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
using UnityEngine; using System.Collections; using System.Linq; using System.Xml.Linq; using System;public class XML { //static string xmlpath = Application.persistentDataPath + @"\myXML";//平臺(tái)相關(guān)的路徑(移動(dòng)端) static string xmlpath=Application.dataPath+@"\mydfdfXML";//電腦上的路徑,移動(dòng)端沒(méi)有這個(gè)訪問(wèn)權(quán)限 /// <summary> /// 初始化一個(gè)XML文件 /// </summary> public static void CreateXMLDocument() { XElement root = new XElement("XMLContent", new XElement("Herb1",new XAttribute("MyVaule","0")), new XElement("Herb2",new XAttribute("MyVaule","0")), new XElement("Herb3",new XAttribute("MyVaule","0")), new XElement("Pill1",new XAttribute("MyVaule","0")), new XElement("Pill2",new XAttribute("MyVaule","0")), new XElement("Pill3",new XAttribute("MyVaule","0")), new XElement("Level",new XAttribute("MyVaule","0")), new XElement("Root","root") ); root.Save(xmlpath); } public static XElement LoadXMLFromFile() { XElement root = XElement.Load(xmlpath); return root; } public static void SetElementValue(string name, string value) { XElement root = LoadXMLFromFile(); root.Element(name).SetAttributeValue("MyVaule", value); root.Save(xmlpath); } /// <summary> /// 在根節(jié)點(diǎn)元素之前添加新的元素 /// </summary> /// <param name="name">元素名字</param> /// <param name="value">元素的值</param> public static void AddElement(string name, string value) { XElement root = LoadXMLFromFile(); root.Element("Root").AddBeforeSelf(new XElement(name, new XAttribute("MyValue",value))); root.Save(xmlpath); } /// <summary> /// 刪除指定的元素 /// </summary> /// <param name="name">要?jiǎng)h除的元素名稱(chēng)</param> public static void RemoveElement(string name) { XElement root = LoadXMLFromFile(); root.Element(name).Remove(); root.Save(xmlpath); } /// <summary> /// 根據(jù)元素名查找元素對(duì)應(yīng)的值 /// </summary> /// <param name="name">元素名</param> /// <returns></returns> public static string GetElementValue(string name) { XElement root = LoadXMLFromFile(); XAttribute xattr = root.Element(name).Attribute("MyVaule"); string s = xattr.Value; return s; } }

?

http://blog.csdn.net/lyq5655779/article/details/7183350

1.寫(xiě)XML文件

[html]?view plaincopy
  • XElement?xperson?=?new?XElement("person");//根節(jié)點(diǎn)??
  • ????????????xperson.SetAttributeValue("age",?30);//設(shè)置屬性??
  • ????????????XElement?xperson1?=?new?XElement("person1");??
  • ????????????xperson1.Value?=?"tom";//設(shè)置?innerText值??
  • ????????????xperson1.SetAttributeValue("sex",?"男");??
  • ????????????XElement?xperson2?=?new?XElement("person2");??
  • ??
  • ????????????xperson.Add(xperson1);//加入到根結(jié)點(diǎn)??
  • ????????????xperson.Add(xperson2);??
  • ????????????string?xml?=?xperson.ToString();??
  • ????????????Console.WriteLine(xml);??
  • ????????????using?(Stream?stream?=?File.OpenWrite(@"D:\1.xml"))??
  • ????????????{??
  • ????????????????byte[]?bytes?=?new?byte[1024];??
  • ????????????????bytes?=?Encoding.UTF8.GetBytes(xml);??
  • ????????????????stream.Write(bytes,?0,?bytes.Length);//寫(xiě)入文件??
  • ????????????}??

  • 2.讀取XML文件

    ? ----------如何讀取xml文件的內(nèi)容----------------

    ??????????? using(Stream stream=File.OpenRead(@"D:\1.xml"))
    ??????????? {
    ?????????????? using(StreamReader reader= new StreamReader(stream))
    ?????????????? {
    ?????????????????? //從Reflector從可以看到TextReader的子類(lèi)是StreamReader所以,可以直接用StreamReader來(lái)讀取XML文檔,傳給XDocument.Load方法
    ????????????????? XDocument xml=XDocument.Load(reader);//load里面
    ????????????????? Console.WriteLine( xml.Root.ToString());//xml文檔
    ????????????????? Console.WriteLine(xml.Root.Nodes().ElementAt(0).ToString());//ElementAt()第幾個(gè)子節(jié)點(diǎn)?
    ????????????????? Console.WriteLine(xml.Root.Attribute("age").Value);//返回根節(jié)點(diǎn)的屬性的值
    ????????????????? XNode nodes=xml.Root.Nodes().ElementAt(0);
    ????????????????? XElement e=nodes as XElement;
    ????????????????? Console.WriteLine(e.Attribute("sex").Value);//讀取第一個(gè)子節(jié)點(diǎn)的屬性
    ??????????????? }
    ????????????
    ??????????? }

    3.讀取App.Config

    ?

    ? using (Stream stream = File.OpenRead(@"D:\net實(shí)例教程\練習(xí)net\XML練習(xí)\XML練習(xí)\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 .....>第一個(gè)
    ??????????????????? XNode n1_2 = e1.Nodes().ElementAt(1);//connectionStrings下面的<add .....>第一個(gè)
    ??????????????????? 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("第一個(gè)連接字符串的name={0},Connection={1}", e1_1.Attribute("name"), e1_1.Attribute("connectionString"));

    ??????????????????? Console.WriteLine("第一個(gè)連接字符串的name={0},Connection={1}", e1_2.Attribute("name"), e1_2.Attribute("connectionString"));


    ??????????????? }
    ??????????? }
    ???????


    ?????????

    [csharp]?view plaincopy
  • using?(Stream?stream?=?File.OpenRead(@"D:\net實(shí)例教程\練習(xí)net\XML練習(xí)\XML練習(xí)\App.config"))??
  • {??
  • ??
  • ????using?(StreamReader?reader?=?new?StreamReader(stream))??
  • ????{???
  • ????????XDocument?xml=XDocument.Load(reader);??
  • ????????IEnumerable<XElement>?XConnstr?=?xml.Root.Elements("connectionStrings");??
  • ??????
  • ????????Console.WriteLine(XConnstr.Count().ToString());??
  • ????????if?(XConnstr.Count()?==?1)??
  • ????????{??
  • ?????????????XElement?Connstr?=?XConnstr.ElementAt(0);??
  • ????????????foreach(XElement?conn?in?Connstr.Elements("add"))??
  • ????????????{??
  • ????????????????string?name?=?conn.Attribute("name").Value;??
  • ????????????????string?Connection?=?conn.Attribute("connectionString").Value;??
  • ????????????????Console.WriteLine("Name={0},Age={1}",?name,?Connection);??
  • ??????????????
  • ????????????}??
  • ??????????
  • ????????}??
  • ????}??
  • ??
  • }?
  • 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>任務(wù)創(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>化驗(yàn)</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();}//如果當(dāng)前元素的子元素只有一個(gè),可以直接用.Element("elementName")來(lái)得到,如果是多個(gè)子元素就用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的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。