您的位置:首页 > 其它

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: