linq to xml
2012-01-22 09:43
309 查看
一 private static string name, num;
public static string Num
{
get { return SysConfig.num; }
set { SysConfig.num = value; }
}
public static string Name
{
get { return SysConfig.name; }
set { SysConfig.name = value; }
}
public static void CreateXML(string filepath)
{
StudentDataContext studc = new StudentDataContext();
var stu = from s in studc.Student
select s;
XElement stuxml = new XElement("Students");
foreach (Student s in stu)
{
stuxml.Add(new XElement("student",
new XAttribute("Name", s.StuName),
new XElement("Number", s.StuNum)));
}
stuxml.Save(filepath);
}
public static XDocument doc;
public static void ReadDefaultValue(string filepath)
{
// 有很多student标签,默认为第一个
try
{
doc = XDocument.Load(filepath);
num = doc.Element("Students").Element("student").Value;
name = doc.Element("Students").Element("student").Attribute("Name").Value;
}
catch
{
// 如果try里有错,也不影响运行
num = "100000";
name = "xy";
}
}
public static IEnumerable<XElement> Find(string filepath)
{
doc = XDocument.Load(filepath);
var result = from r in doc.Element("Students").Elements("student")
where (string)r.Attribute("Name").Value == "xy"
select r;
return result;
}
public static void Remove(string filepath)
{
doc = XDocument.Load(filepath);
doc.Element("Students").Elements("student").Where(ss => ss.Attribute
("Name").Value.ToString() == "xy").Remove();
doc.Save(filepath);
}
public static void Add(string filepath)
{
doc = XDocument.Load(filepath);
doc.Element("Students").Add(new XElement("student",
new XAttribute("Name", "小明"),
new XElement("Number", "111")));
doc.Save(filepath);
}
public static void SetValue(string filepath)
{
doc = XDocument.Load(filepath);
doc.Element("Students").Elements("student").Where(ee => (string)ee.Attribute("Name")
== "xy").FirstOrDefault().SetAttributeValue("Name", "xy");
doc.Element("Students").Elements("student").Where(ee => (string)ee.Attribute("Name")
== "xy").FirstOrDefault().SetElementValue("Number", "100000");
doc.Save(filepath);
}
}
二.
public static class BlackWhite
{
//private static string FindConfigFile(string fileName)
//{
// //return HttpContext.Current.Server.MapPath(string.Format("~/Config/{0}",
fileName)); // 找到XML文件路径
//}
private static IEnumerable<string> Black;
public static IEnumerable<string> Black1
{
get { return BlackWhite.Black; }
set { BlackWhite.Black = value; }
}
private static IEnumerable<string> White;
public static IEnumerable<string> White1
{
get { return BlackWhite.White; }
set { BlackWhite.White = value; }
}
public static void UseXml()
{
//XDocument doc = XDocument.Load(FindConfigFile("BlackWhite.xml"));
string str = System.Environment.CurrentDirectory;
XDocument doc = XDocument.Load(string.Format(@"{0}\BlackWhite.xml", str));
Black = doc.Element("List").Element("Black").Elements("Name").Select(element =>
element.Value);
White = doc.Element("List").Element("White").Elements("Name").Select(element =>
element.Value);
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (Black.Contains(p.ProcessName))
{
p.Kill();
MessageBox.Show("上课请不要打开无关程序");
}
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<List>
<Black>
<Name>QQ</Name>
<Name>AliIm</Name>
</Black>
<White>
<Name>SeverUDP</Name>
<Name>ClientUDP</Name>
</White>
</List>
public static string Num
{
get { return SysConfig.num; }
set { SysConfig.num = value; }
}
public static string Name
{
get { return SysConfig.name; }
set { SysConfig.name = value; }
}
public static void CreateXML(string filepath)
{
StudentDataContext studc = new StudentDataContext();
var stu = from s in studc.Student
select s;
XElement stuxml = new XElement("Students");
foreach (Student s in stu)
{
stuxml.Add(new XElement("student",
new XAttribute("Name", s.StuName),
new XElement("Number", s.StuNum)));
}
stuxml.Save(filepath);
}
public static XDocument doc;
public static void ReadDefaultValue(string filepath)
{
// 有很多student标签,默认为第一个
try
{
doc = XDocument.Load(filepath);
num = doc.Element("Students").Element("student").Value;
name = doc.Element("Students").Element("student").Attribute("Name").Value;
}
catch
{
// 如果try里有错,也不影响运行
num = "100000";
name = "xy";
}
}
public static IEnumerable<XElement> Find(string filepath)
{
doc = XDocument.Load(filepath);
var result = from r in doc.Element("Students").Elements("student")
where (string)r.Attribute("Name").Value == "xy"
select r;
return result;
}
public static void Remove(string filepath)
{
doc = XDocument.Load(filepath);
doc.Element("Students").Elements("student").Where(ss => ss.Attribute
("Name").Value.ToString() == "xy").Remove();
doc.Save(filepath);
}
public static void Add(string filepath)
{
doc = XDocument.Load(filepath);
doc.Element("Students").Add(new XElement("student",
new XAttribute("Name", "小明"),
new XElement("Number", "111")));
doc.Save(filepath);
}
public static void SetValue(string filepath)
{
doc = XDocument.Load(filepath);
doc.Element("Students").Elements("student").Where(ee => (string)ee.Attribute("Name")
== "xy").FirstOrDefault().SetAttributeValue("Name", "xy");
doc.Element("Students").Elements("student").Where(ee => (string)ee.Attribute("Name")
== "xy").FirstOrDefault().SetElementValue("Number", "100000");
doc.Save(filepath);
}
}
二.
public static class BlackWhite
{
//private static string FindConfigFile(string fileName)
//{
// //return HttpContext.Current.Server.MapPath(string.Format("~/Config/{0}",
fileName)); // 找到XML文件路径
//}
private static IEnumerable<string> Black;
public static IEnumerable<string> Black1
{
get { return BlackWhite.Black; }
set { BlackWhite.Black = value; }
}
private static IEnumerable<string> White;
public static IEnumerable<string> White1
{
get { return BlackWhite.White; }
set { BlackWhite.White = value; }
}
public static void UseXml()
{
//XDocument doc = XDocument.Load(FindConfigFile("BlackWhite.xml"));
string str = System.Environment.CurrentDirectory;
XDocument doc = XDocument.Load(string.Format(@"{0}\BlackWhite.xml", str));
Black = doc.Element("List").Element("Black").Elements("Name").Select(element =>
element.Value);
White = doc.Element("List").Element("White").Elements("Name").Select(element =>
element.Value);
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (Black.Contains(p.ProcessName))
{
p.Kill();
MessageBox.Show("上课请不要打开无关程序");
}
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<List>
<Black>
<Name>QQ</Name>
<Name>AliIm</Name>
</Black>
<White>
<Name>SeverUDP</Name>
<Name>ClientUDP</Name>
</White>
</List>
相关文章推荐
- LINQ to XML
- LINQ to XML 用 LINQ 查询 XML
- linq to xml查找元素的值
- Linq To Xml 创建修改xml文档
- Linq to Xml下类似XML DOM中InnerXml的操作
- [引]LINQ to XML 类概述
- Read XML file easily with LINQ to XML
- .net与XML——LINQ To Xml
- LINQ to XML 介绍(转)
- [代码]如何从文件加载XML(LINQ to XML)
- [代码]使用XmlReader对XML片段进行流式处理(LINQ to XML)
- LinQ To XML——用LinQ查询XML
- LINQ to XML
- 使用linq to xml 快速创建自己的Rss
- Linq to SQL Xml Based Mapping
- 使用linq to xml 快速创建自己的Rss
- LINQ TO XML之判断不存在行则插入
- LINQ To XML 入门(1)
- LINQ to XML实现XML文档的增删改查
- linq to xml 基本操作