using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.Data;
using System.Linq;
using System.IO;
using System.Xml;

namespace OurMsg.Class.common


{

/**//// <summary>
/// XML 格式轉(zhuǎn)換類(lèi)
/// </summary>
public class XmlConvert

{

DeCode#region DeCode

/**//// <summary>
/// 檢索單個(gè)子元素
/// </summary>
/// <param name="xmlData">xml 數(shù)據(jù)</param>
/// <param name="key">鍵名</param>
/// <returns></returns>
public static string ToString(string xmlData, string key)

{
try

{
XElement els = XElement.Parse(xmlData);
XElement e = els.Element(key);
return (e != null) ? e.Value : "";
}
catch (Exception)

{
return "";
}
}


/**//// <summary>
/// xml數(shù)據(jù)轉(zhuǎn)換到Dictionary(鍵/值)
/// </summary>
/// <param name="xmlData">xml數(shù)據(jù)</param>
/// <returns></returns>
public static Dictionary<string, string> ToDictionary(string xmlData)

{
try

{
Dictionary<string, string> dict = new Dictionary<string, string>();
XElement els = XElement.Parse(xmlData);
foreach (XElement el in els.Elements())
dict.Add(el.Name.LocalName, el.Value);
return dict;
}
catch (Exception)

{
return new Dictionary<string, string>();
}
}


/**//// <summary>
/// 將 xml數(shù)據(jù)轉(zhuǎn)換為DataSet
/// </summary>
/// <param name="xmlData">xml數(shù)據(jù)</param>
/// <returns></returns>
public static DataSet ToDataSet(string xmlData)

{
StringReader stream = null;
XmlTextReader reader = null;
DataSet xmlDS = new DataSet();
try

{
stream = new StringReader(xmlData);
//從stream裝載到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader); //ps.此方法不支持 XmlReadMode.IgnoreSchema
}
catch (Exception)

{
}
finally

{
if (reader != null) reader.Close();
}

//保持?jǐn)?shù)據(jù)有效
if (xmlDS.Tables.Count==0)

{
DataTable table = new DataTable();
xmlDS.Tables.Add(table);
}

return xmlDS;
}
#endregion


EnCode#region EnCode

/**//// <summary>
/// 單個(gè)數(shù)據(jù)生成 xml
/// </summary>
/// <param name="key">鍵</param>
/// <param name="value">值</param>
/// <returns></returns>
public static string ToXmlData(string key, string value)

{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("root",
new XElement(key, value)
)
);

return doc.ToString();
}


/**//// <summary>
/// 集合數(shù)據(jù)生成 xml數(shù)據(jù)
/// </summary>
/// <param name="dictSend">Dictionary 集合</param>
/// <returns></returns>
public static string ToXmlData(Dictionary<string, string> dict)

{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("root",
from keyValue in dict
select new XElement(keyValue.Key, keyValue.Value)
)
);

return doc.ToString();
}
#endregion


/**//// <summary>
/// xml傳輸 時(shí)間格式的一點(diǎn)格式化(建議取消)
/// </summary>
/// <param name="dtList"></param>
/// <param name="key"></param>
/// <param name="format"></param>
public static void DataTimeToString(DataTable dt, string key, string format)

{
foreach (DataRow row in dt.Rows)
row[key] = Convert.ToDateTime(row[key]).ToString(format);
}
}
}

posted on 2009-07-15 15:09
黃小二 閱讀(335)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
ASP.NET 、
C#