linq to xml实战
直接上代码: 比如要生成象下面那样的xml文件: <?xml version="1.0" encoding="utf-8"?>
<tags>
<tag id="1">
<tagid>1</tagid>
<tagname>ibm专区云标签</tagname>
<tagheight>200</tagheight>
<tagwidth>300</tagwidth>
<cloudxmlfile>tagcloud1.xml</cloudxmlfile>
</tag>
<tag id="2">
<tagid>2</tagid>
<tagname>CAD专题</tagname>
<tagheight>300</tagheight>
<tagwidth>500</tagwidth>
<cloudxmlfile>tagcloud2.xml</cloudxmlfile>
</tag>
</tags>
可以象下面这样写: public void AddTagNameXml(string tagname,string tagheight,string tagwidth)
{
string xmlFilePath = "/xml/tagcloudnamexml.xml"; //这个地方还应该加上验证文件存不存在的代码,这里省略
XElement xe = XElement.Load(HttpContext.Current.Server.MapPath(xmlFilePath));
int tagid = 0;
int topxecount= xe.Elements("tag").Count();
//看是否存在tag节点
if (topxecount > 0) {
XElement tagidelement = xe.Elements("tag").Last().Element("tagid");
//给tag标签tag节点的属性id+1
tagid = int.Parse(tagidelement.Value) + 1;
}
else
tagid = 1;
//给云标签xml文件取名
string tagcloudXmlName = "tagcloud" + tagid + ".xml";
XElement tag = new XElement("tag",new XAttribute("id", tagid), new XElement("tagid", tagid), new XElement("tagname", tagname),
new XElement("tagheight", tagheight), new XElement("tagwidth", tagwidth),
new XElement("cloudxmlfile", tagcloudXmlName) );
xe.Add(tag);
xe.Save(HttpContext.Current.Server.MapPath(xmlFilePath));
}
//修改
private void ModifyXmlElement(string tagid, string tagname, string tagheight, string tagwidth)
{
string xmlFilePath = "/xml/tagcloudnamexml.xml";
XElement xe = XElement.Load(HttpContext.Current.Server.MapPath(xmlFilePath));
IEnumerable<XElement> element = from e in xe.Elements("tag")
where e.Attribute("id").Value == tagid select e;
if (element.Count() > 0)
{
XElement firstelement = element.First();
firstelement.Attribute("tagname").Value = tagname;
firstelement.Attribute("tagheight").Value = tagheight;
firstelement.Attribute("tagwidth").Value = tagwidth;
}
xe.Save(HttpContext.Current.Server.MapPath(xmlFilePath));
}
/// <summary> /// 删除 /// </summary>
/// <param name="attrvalue">节点属性值,用来查找要删除的节点</param>
/// <param name="xmlPath">xml文件路径</param>
/// <param name="elementname">节点名称</param>
/// <param name="attrname">属性名称</param>
private void DeleteXmlElement(string attrvalue,string xmlPath,string elementname,string attrname)
{
XElement xe = XElement.Load(HttpContext.Current.Server.MapPath(xmlPath));
IEnumerable<XElement> element = from e in xe.Elements(elementname) where e.Attribute(attrname).Value == attrvalue select e;
if (element.Count() > 0)
{
element.Remove();
}
xe.Save(HttpContext.Current.Server.MapPath(xmlPath));
}
再比如要生成下面的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<tags>
<tag tagid="2" tagcontent="智能企业" tagurl="http://ibm.e-works.net.cn/document/200908/article8963.htm" />
<tag tagid="3" tagcontent="中小企业" tagurl="http://ibm.e-works.net.cn/document/200908/article8969.htm" />
<tag tagid="4" tagcontent="中望cad2" tagurl="http://www.e-works.net.cn" />
<tag tagid="5" tagcontent="" tagurl="" />
<tag tagid="6" tagcontent="yy" tagurl="dgd" /> </tags>
private string AddCloudTagXmlForList(string tagcontent,tstring agurl)
{
string xmlFilePath = "/xml/tagcloudxml.xml";
XElement tag = XElement.Load(PathHelper.Map(xmlpath));
int topxecount = tag.Elements("tag").Count();
int tagid = 0;
if (topxecount > 0) {
string maxtagid = tag.Elements("tag").Last().Attribute("tagid").Value;
tagid = int.Parse(maxtagid) + 1; }
else
tagid = 1;
tag.Add(new XElement("tag", new XAttribute("tagid", tagid), new XAttribute("tagcontent", tagcontent), new XAttribute("tagurl", tagurl))); tag.Save(PathHelper.Map(xmlpath));
return xmlpath;
}
//修改
private void ModifyCloudTagXmlForListElement(string tagcontent,string tagurl,string xmlpath)
{
XElement tag = XElement.Load(PathHelper.Map(xmlpath));
IEnumerable<XElement> element = from e in tag.Elements("tag")
where e.Attribute("tagid").Value == ctag.tagid.ToString() select e;
if (element.Count() > 0)
{
XElement firstelement = element.First();
firstelement.Attribute("tagcontent").SetValue(tagcontent);
firstelement.Attribute("tagurl").SetValue(tagurl);
}
tag.Save(PathHelper.Map(xmlpath));
}
再比如象下面形式的xml
<?xml version="1.0" encoding="utf-8"?>
<tags> <a tagid="2" href="http://ibm.e-works.net.cn/document/200908/article8963.htm" style="font-size:18pt;">智能企业</a>
<a tagid="3" href="http://ibm.e-works.net.cn/document/200908/article8969.htm" style="font-size:18pt;">中小企业</a>
<a tagid="4" href="http://www.e-works.net.cn" style="font-size:18pt;">中望cad2</a>
</tags>
public void AddCloudTagXml(string tagcontent,string tagurl)
{
XElement xe = XElement.Load(PathHelper.Map(xmlpath));
int topxecount = xe.Elements("a").Count();
int tagid = 0;
if (topxecount > 0)
{
string maxtagid = xe.Elements("a").Last().Attribute("tagid").Value;
tagid = int.Parse(maxtagid) + 1;
}
else
tagid = 1;
XElement tagadd = new XElement("a", new XAttribute("tagid", tagid), new XAttribute("href", tagurl), new XAttribute("style", "font-size:18pt;"), tagcontent);
xe.Add(tagadd);
xe.Save(PathHelper.Map(xmlpath));
}
//修改 private void ModifyCloudTagXmlElement(string tagcontent,string tagurl, string xmlpath)
{
XElement tag = XElement.Load(PathHelper.Map(xmlpath));
IEnumerable<XElement> element = from e in tag.Elements("a")
where e.Attribute("tagid").Value == ctag.tagid.ToString() select e;
if (element.Count() > 0) {
XElement firstelement = element.First();
firstelement.SetValue(tagcontent);
firstelement.Attribute("href").SetValue(tagurl);
}
}
tag.Save(PathHelper.Map(xmlpath));
}