Unity上使用Linq To XML
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Xml.Linq;
using System; public class XML {
//static string xmlpath = Application.persistentDataPath + @"\myXML";//平台相关的路径(移动端)
static string xmlpath=Application.dataPath+@"\mydfdfXML";//电脑上的路径,移动端没有这个访问权限
/// <summary>
/// 初始化一个XML文件
/// </summary>
public static void CreateXMLDocument()
{
XElement root = new XElement("XMLContent",
new XElement("Herb1",new XAttribute("MyVaule","")),
new XElement("Herb2",new XAttribute("MyVaule","")),
new XElement("Herb3",new XAttribute("MyVaule","")),
new XElement("Pill1",new XAttribute("MyVaule","")),
new XElement("Pill2",new XAttribute("MyVaule","")),
new XElement("Pill3",new XAttribute("MyVaule","")),
new XElement("Level",new XAttribute("MyVaule","")),
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>
/// 在根节点元素之前添加新的元素
/// </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">要删除的元素名称</param>
public static void RemoveElement(string name)
{
XElement root = LoadXMLFromFile();
root.Element(name).Remove();
root.Save(xmlpath);
}
/// <summary>
/// 根据元素名查找元素对应的值
/// </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.写XML文件
- XElement xperson = new XElement("person");//根节点
- xperson.SetAttributeValue("age", 30);//设置属性
- XElement xperson1 = new XElement("person1");
- xperson1.Value = "tom";//设置 innerText值
- xperson1.SetAttributeValue("sex", "男");
- XElement xperson2 = new XElement("person2");
- xperson.Add(xperson1);//加入到根结点
- 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);//写入文件
- }
2.读取XML文件
----------如何读取xml文件的内容----------------
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()第几个子节点
Console.WriteLine(xml.Root.Attribute("age").Value);//返回根节点的属性的值
XNode nodes=xml.Root.Nodes().ElementAt(0);
XElement e=nodes as XElement;
Console.WriteLine(e.Attribute("sex").Value);//读取第一个子节点的属性
}
}
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"));
}
}
- using (Stream stream = File.OpenRead(@"D:\net实例教程\练习net\XML练习\XML练习\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>任务创建</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") == workflowName
select
new{
str = workflow.Elements("activity").TakeWhile(x => (string)x.Attribute("name") == stepName).First().Element(typeName).Element(pageMethod).Value
};
return strEle.First().str;
}
}
}
Unity上使用Linq To XML的更多相关文章
- Linq to Xml读取复杂xml(带命名空间)
前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...
- c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)
主界面
- C# ~ 从 XML 到 Linq 到 Linq to XML
.XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...
- 不挣扎了,开始学习LINQ TO XML,进而来解析网页。
找到了别人遇到和我一样的问题:http://ylad.codeplex.com/discussions/430095(英文) 一位叫做Mister Goodcat的提供了信息: Short answe ...
- Linq学习笔记---Linq to Xml操作
LINQ to XML的成员, 属性列表: 属性 说明 Document 获取此 XObject 的 XDocument EmptySequence 获取空的元素集合 FirstAttribut ...
- C#学习之Linq to Xml
前言 我相信很多从事.NET开发的,在.NET 3.5之前操作XML会比较麻烦,但是在此之后出现了Linq to Xml,而今天的主人公就是Linq to Xml,废话不多说,直接进入主题. 题外:最 ...
- C#中的Linq to Xml详解
这篇文章主要介绍了C#中的Linq to Xml详解,本文给出转换步骤以及大量实例,讲解了生成xml.查询并修改xml.监听xml事件.处理xml流等内容,需要的朋友可以参考下 一.生成Xml 为了能 ...
- LINQ TO XML 个人的一些心得1
最近没事做,刚来到一个新公司.写了一些处理xml的项目 就是把一些xml的数据处理后存储到数据库中.原本还是准备用原来的xml来写的.在群里有个人说,用linq to xml 好了,比较快捷.就看了 ...
- 24.C#LINQ TO XML(十二章12.3)
自己也写了那么多,但还有很多不懂,有点浮躁吧,但饭还是要吃啊,说说LINQ TO XML吧. LINQ TO XML位于System.Xml.Linq程序集,并且大多数类型位于System.Xml.L ...
随机推荐
- SQL Server 基础:子查询
1.子查询的概念:子查询就是嵌套在主查询中的查询.子查询可以嵌套在主查询中所有位置,包括SELECT.FROM.WHERE.GROUP BY.HAVING.ORDER BY.2.子查询的分类:2.1按 ...
- 第二十一章 数据访问(In .net4.5) 之 序列化
1. 概述 应用程序间传递数据,需要先将数据对象转化为字符流或字节流的形式,然后接收端收到后再转化回原始的数据对象.这就是序列化与反序列化. 本章介绍 .net中的序列化与反序列化.序列化器的种类 以 ...
- 用Python作GIS之五:从示例入手—example函数
进入STARS后,最简单的学习方法就是演示示例数据.对于源码的分析也可以从这里入手. 以下为出发菜单项“Example Project”的函数example:def example(se ...
- Swift TabeleViewCell dequeueReusableCellWithIdentifier 使用的新的细节,原来现在可以这样
今天在看官方的TableView Guide,突然想起来最近写的一个代码中实现tableViewCell复用的时候有点问题: var cell = UITableViewCell(style: UIT ...
- Windows Server 2003服务器.net4.0+IIS6.0的服务器,IE11浏览器访问的不兼容性
工作中发生了一件诡异的事情: 程序在Win7+.NET4.0+IIS7.5的服务器部署,IE8和IE11请求时,响应的样式都正常. 但是在美的同事反映说,Windows Server 2003服务器. ...
- hmmer 使用(转载)
hmmer 使用 » 转载文章请注明,转载自:博耘生物 » <hmmer的安装与使用> » 原文链接:http://boyun.sh.cn/bio/?p=1753 从功能基因研究的角度 ...
- 16.如何设置Quartus II Programmer,保护pof不被读出
Program时,把security bit勾上,点击start 这样examine时就不能正确的读出pof 读出来的pof 除文件头外,其余的内容全为0 怎么样,大家试试!
- Android实现入门界面布局
Android实现入门界面布局 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是常量的定义,安卓中固定字符串应该定义在常量中. stri ...
- C语言中函数有输出参数
#include "stdio.h"void set(int *ch,int a,int *c,int *d){ c[0]=12; c[1]=133; c[2]=14; ch[0] ...
- 数组和字典 swift
var array = ["A","B"] var array2:[String] = ["A","B"] var ar ...