Linq To Xml操作类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Net;

/*Xml操作类:Ruiky 2011-12-28*/
namespace FinanceGrab.Common.Util
{
    public class XmlUtil
    {
        private static object Syncr = new object();
        

View Code
/// <summary>
/// 写入Xml数据
/// </summary>
/// <param name="xmlName">Xml名称</param>
/// <param name="grabName">抓取产品名称</param>
/// <param name="standardName">库中名称</param>
/// <param name="proCode">产品代码</param>
/// <param name="remark">备注</param>
/// <returns>True:成功,False:失败</returns>
public static bool InsertXml(string xmlName, string websiteID, string grabName, string standardName, string proCode, string remark)
{
lock (Syncr)
{
try
{
XDocument doc = new XDocument();
string xmlFilePath = Directory.GetParent(Assembly.GetExecutingAssembly().Location) + @"\" + xmlName;

if (!File.Exists(xmlFilePath))
{
doc.Add(new XElement("Websites", ""));
doc.Save(xmlFilePath);
}

doc = XDocument.Load(xmlFilePath);
bool b = UpdateXml(xmlFilePath,websiteID, grabName, standardName, proCode, remark);
if (!b)
{
XElement xe = new XElement("Website", new XAttribute("siteId", websiteID),
new XElement("Product",
new XAttribute("GrabName", grabName),
new XAttribute("Code", proCode),
new XAttribute("StandardName", standardName),
new XAttribute("Remark", remark),
new XAttribute("AddTime", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")),
new XAttribute("HostName", Dns.GetHostName())
));

doc.Element("Websites").Add(xe);
doc.Save(xmlFilePath);
return true;
}

return b;

}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}

/// <summary>
/// 更新Xml数据
/// </summary>
/// <param name="xmlFilePath">Xml路径名称</param>
/// <param name="grabName">抓取产品名称</param>
/// <param name="standardName">库中名称</param>
/// <param name="proCode">产品代码</param>
/// <param name="remark">备注</param>
/// <returns>True:成功,False:失败</returns>
public static bool UpdateXml(string xmlFilePath, string websiteID, string grabName, string standardName, string proCode, string remark)
{
lock (Syncr)
{
try
{
XDocument doc = XDocument.Load(xmlFilePath);

if (doc != null)
{
var vIsCreatWebsite = (from g in doc.Elements().Descendants("Website")/*是否创建网站记录:网站唯一性*/
where g.Attribute("siteId").Value.Equals(websiteID)
select g).FirstOrDefault();



if (vIsCreatWebsite != null)
{
var vIsCreatProduct = (from g in vIsCreatWebsite.Descendants("Product")/*是否创建产品记录:产品唯一性*/
where g.Attribute("GrabName").Value.Equals(grabName)
select g).FirstOrDefault();

if (vIsCreatProduct == null)
{
XElement xe = new XElement("Product");
xe.SetAttributeValue("GrabName", grabName);
xe.SetAttributeValue("Code", proCode);
xe.SetAttributeValue("StandardName", standardName);
xe.SetAttributeValue("Remark", remark);
xe.SetAttributeValue("AddTime", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
xe.SetAttributeValue("HostName", Dns.GetHostName());

vIsCreatWebsite.Add(xe);
doc.Save(xmlFilePath);

return true;
}
else
{
vIsCreatProduct.SetAttributeValue("Code", proCode);
vIsCreatProduct.SetAttributeValue("Remark", remark);

doc.Save(xmlFilePath);
return true;
}
}
}

return false;

}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}

 

    }
}

posted @ 2011-12-29 13:34  Ruiky  阅读(350)  评论(0编辑  收藏  举报