110,535
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8" ?>
<xmlboot>
<data id="10000" type="up" num="20" bianli="0"/>
<data id="10001" type="down" num="20" bianli="0"/>
<data id="10002" type="up" num="20" bianli="0"/>
..........
<data id="11000" type="up" num="20" bianli="0"/>
</xmlboot>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XML_Example
{
class about
{
/// <summary>
/// 查看XML内容
/// </summary>
/// <param name="path">XML文档路径</param>
/// <param name="node">查找的节点</param>
public static void ShowXmlInfo(string path,string Nodes)
{
XmlDocument doc = new XmlDocument();
doc.Load("xml_test.xml");//path
XmlElement root = doc.DocumentElement;
Console.Write(root.SelectNodes("book")[2].InnerText);//Nodes
}
/// <summary>
/// 添加信息
/// </summary>
/// <param name="path">XML文档路径</param>
/// <param name="Nodes">添加的父节点</param>
public static void AddXmlInfo(string path, string Nodes)
{
XmlDocument doc = new XmlDocument();
doc.Load("xml_test.xml");//path
XmlNode root = doc.SelectSingleNode("bookstore");//Nodes
XmlElement xe1 = doc.CreateElement("book");
xe1.SetAttribute("genre", "李赞红");
xe1.SetAttribute("ISBN", "2-3631-4");
XmlElement xesub1 = doc.CreateElement("title");
xesub1.InnerText = "C#入门经典";
xe1.AppendChild(xesub1);
XmlElement xesub2 = doc.CreateElement("author");
xesub2.InnerText = "贱人";
xe1.AppendChild(xesub2);
XmlElement xesub3 = doc.CreateElement("price");
xesub3.InnerText = "60";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);
doc.Save("xml_test.xml");
}
/// <summary>
/// 修改信息
/// </summary>
/// <param name="path">路径</param>
/// <param name="Nodes">节点</param>
public static void ReviseXmlInfo(string path, string Nodes)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("xml_test.xml");
XmlNodeList nodelist = xmldoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in nodelist)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "李赞红")
{
xe.SetAttribute("genre", "LiZanHong");
XmlNodeList nls = xe.ChildNodes;
foreach (XmlNode _xn in nls)
{
XmlElement xe2 = (XmlElement)_xn;
if (xe2.Name == "author")
{
xe2.InnerText = "Jb";
}
break;
}
break;
}
}
xmldoc.Save("xml_test.xml");
}
/// <summary>
/// 删除XML信息
/// </summary>
/// <param name="path">XML文档路径</param>
/// <param name="Nodes">节点</param>
public static void DeleteXmlInfo(string path, string Nodes)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("xml_test.xml");
XmlNodeList xnl = xmldoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "fantasy")
{
xe.RemoveAttribute("genre");
}
else if (xe.GetAttribute("genre") == "LiZanHong")
{
xe.RemoveAll();
}
}
xmldoc.Save("xml_test.xml");
}
}
}
string xmlpath = Environment.CurrentDirectory + @"\a.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlpath);
XmlNodeList xnl = doc.SelectNodes("xmlboot/data[@bianli='0']");
int countResult = xnl.Count;
Random r = new Random((Int32)System.DateTime.Now.Ticks);
int currentIndex = r.Next(countResult);
XmlElement xe = (XmlElement)xnl[currentIndex];
xe.SetAttribute("bianli", "1");
doc.Save(xmlpath);
static void Main(string[] args)
{
string xmlFileName = @"D:\VisualStudioProject2008\CSDNAnswer\Linq2Xml_RadomAccess\Linq2Xml_RadomAccess\data.xml";
XElement xmldoc = XElement.Load(xmlFileName);
int beginId = 10000;
int endId = 10003;
// create a list for iterator (make sure that random id is unique)
List<string> ids = new List<string>();
for (int i = beginId; i <= endId; i++)
ids.Add(i.ToString());
// set iterator times
int selectTimes = 2;
while (selectTimes > 0)
{
Random random = new Random(System.Guid.NewGuid().GetHashCode());
string selectId = ids[random.Next(0, ids.Count-1)];
var item = (from e in xmldoc.Descendants("data")
where e.Attribute("id").Value == selectId
select e).FirstOrDefault<XElement>();
item.Attribute("bianli").Value = "1";
Console.WriteLine(selectId);
ids.Remove(selectId);
selectTimes--;
}
xmldoc.Save(xmlFileName);
Console.Read();
}