Thứ Hai, 28 tháng 1, 2008

Utilities XML

using System;
using System.IO;
using System.Data;
using System.Xml;
using System.Xml.XPath;
using System.Text;
using System.Xml.Xsl;
using System.Collections;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace Lib.Utils
{///
/// Summary description for XMLUtil.
///

public class XMLUtil
{
public XMLUtil()
{
//
// TODO: Add constructor logic here
//
}
public static string TransformFile(string xmlFile, string xslFile)
{
return TransformFile(xmlFile, xslFile, null);
}
public static string TransformFile(string xmlFile, string xslFile, Hashtable paramList)
{
StringWriter sWriter = null;
MemoryStream ms = null;
XmlReader input = null;
XslCompiledTransform xslCompiledTransform = null;
try
{
XPathDocument xpathdocument = new XPathDocument(xmlFile);
XmlDocument stylesheet = new XmlDocument();
stylesheet.Load(xslFile);
//Create the XslCompiledTransform and Load it with the xsltDocument
xslCompiledTransform = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableScript = true;
xslCompiledTransform.Load(stylesheet, settings, new XmlUrlResolver());
StringBuilder sb = new StringBuilder();
//Create StringWriter for transformation output
sWriter = new StringWriter(sb);
//Create the XsltArgumentList.
XsltArgumentList xslArg = null;
if (paramList != null && paramList.Count > 0)
{
xslArg = new XsltArgumentList();
foreach (string param in paramList)
{
xslArg.AddParam(param, "", paramList[param]);
}
}
xslCompiledTransform.Transform(xpathdocument, xslArg, sWriter);
return sb.ToString();
}
finally
{
if (sWriter != null)
{
sWriter.Close();
}
if (ms != null)
{
ms.Close();
}
if (input != null)
{
input.Close();
input = null;
}
xslCompiledTransform = null;
}
}
public static bool IsValidXML(string xml_data) //DM 12.29.02
{
XmlDocument XmlDoc;
try
{
XmlDoc = new XmlDocument();
XmlDoc.LoadXml(xml_data);
return true;
}
catch
{
return false;
}
} //IsValidXML
///
/// Get a list of specified field name's values of input string
///

///
///
///
public static object[] GetFieldValues (string p_szXmlSrc, string p_szSrcFieldName)
{
if (p_szXmlSrc == null p_szXmlSrc.Length == 0)
return null;
object[] objRet = null;
DataSet ds = new DataSet();
StringReader src = new StringReader(p_szXmlSrc);
ds.ReadXml(src);
int n;
try {n = ds.Tables[0].Rows.Count;}
catch {n = 0;}
if (n > 0)
{
objRet = new object[n];
for (int i = 0 ; i < arrsrcfieldname =" new">
/// Get a list of specified field names' values of input string
///
///
///
///
public static object[] GetFieldValues (string p_szXmlSrc, object [] p_arrSrcFieldNames)
{
if (p_szXmlSrc == null p_szXmlSrc.Length == 0
p_arrSrcFieldNames.Length == 0)
return null;
object[] objRet = null;
DataSet ds = new DataSet();
StringReader src = new StringReader(p_szXmlSrc);
ds.ReadXml(src);
if (ds.Tables.Count == 0 ds.Tables[0].Rows.Count == 0)
return null;
int n = ds.Tables[0].Rows.Count;
int nFK = p_arrSrcFieldNames.Length;
if (n > 0)
{
objRet = new object[n];
for (int i = 0 ; i < strvalues =" new" j =" 0">
/// Get a list quoted double of specified field name's values of input string
///
///
///
///
public static string QuotedObject (object[] p_obj)
{
if (p_obj == null p_obj.Length == 0)
return null;
string strQuoted = "", strValue;
for (int i = 0 ; i < strvalue =" p_obj[i].ToString();" strvalue =" p_obj[p_obj.Length-1].ToString();">
/// Get a list quoted single of specified field name's values of input string
///
///
///
///
public static string SingleQuotedObject (object[] p_obj)
{
if (p_obj == null p_obj.Length == 0)
return null;
string strQuoted = "", strValue;
for (int i = 0 ; i < strvalue =" p_obj[i].ToString();" strvalue =" p_obj[p_obj.Length-1].ToString();">
/// Merge two xml string into one with one filed is defined primary field
///
///
///
///
///
/// The return xml string has the same schema with the first input xml string
///

public static string Merge(string p_szFirstXml, string p_szSecondXml, string p_szPKFieldName)
{
object[] primaryField = new object[1];
primaryField[0] = p_szPKFieldName;
return Merge (p_szFirstXml, p_szSecondXml, primaryField);
}
///
/// Merge two xml string into one with one filed is defined primary field
///

///
///
///
///
/// The return xml string has the same schema with the first input xml string
///

public static string Merge(string p_szFirstXml, string p_szSecondXml, object[] p_arrPKFieldNames)
{
DataSet ds1 = new DataSet();
StringReader src = new StringReader(p_szFirstXml);
ds1.ReadXml(src, XmlReadMode.Auto);
DataSet ds2 = new DataSet();
src = new StringReader(p_szSecondXml);
ds2.ReadXml(src, XmlReadMode.Auto);
ds2.DataSetName = ds1.DataSetName;
ds2.Tables[0].TableName = ds1.Tables[0].TableName;
DataColumn[] pk = new DataColumn[p_arrPKFieldNames.Length];
for (int i = 0 ; i < primarykey =" pk;" p_szfirstxml =" RemoveDuplicatedLoans(p_szFirstXml);" ds1 =" new" src =" new" pk =" new" ii =" 0" primarykey =" pk;">" + exp.Message + ". Please contact your System Administrator.");
}
ds1.Merge(ds2, false, MissingSchemaAction.Add);
ds1.AcceptChanges();
return ds1.GetXml();
}
///
/// Merge two xml string into one with one filed is defined primary field
///

///
///
///
///
public static string MergeXML(string PrimaryXML, string AuxiliaryXML, string PrimaryFieldName)
{
object[] primaryField = new object[1];
primaryField[0] = PrimaryFieldName;
return MergeXML (PrimaryXML, AuxiliaryXML, primaryField);
}
///
/// Merge two xml string into one with many fileds are defined primary fields
///

///
///
///
///
public static string DistinctColumns(string srcXML, object[] prikeys_colsname)
{
StringReader src = new StringReader(srcXML);
DataSet ds = new DataSet();
ds.ReadXml(src);
DataTable stbl = ds.Tables[0].Copy();
DataTable dtbl = ds.Tables[0].Copy();
System.Collections.Hashtable hshtbl = new System.Collections.Hashtable();
try
{
string objvalues = "";
int i=0;
foreach(DataRow objRow in stbl.Rows)
{
for(int j=0;j0)
foreach(DataColumn objCCol in MergedDs.Tables[1].Columns)
MergedDs.Tables[0].Rows[j][objCCol.ColumnName]= objChildRow[0][objCCol.ColumnName].ToString();
j++;
}
// Clear all data of the LoanServices and ListOfUsers table
MergedDs.Tables[1].Clear();
// write data to Schema and XML file.
MergedDs.DataSetName = oldDataSetName;
MergedDs.Tables[0].TableName = oldTableName;
Merged_XML = MergedDs.GetXml();
MergedDs.Dispose();
return Merged_XML;
}
catch(Exception e)
{
string strerr = e.Message;
return "";
}
}
///
/// Remove duplicated loans in the input string
///

///
///
public static string RemoveDuplicatedLoans(string p_szXml)
{
//This below block will be customise later to supports for cases in which have many primary keys
object [] p_arrPKFieldNames = new Object[1];
p_arrPKFieldNames[0] = "loan_no";
//end block
//Check duplicated primary key
object[] arrPK = GetFieldValues(p_szXml, p_arrPKFieldNames);
if (arrPK == null)
return p_szXml;
//Change the name of node into "Loan"
StringReader src = new StringReader(p_szXml);
DataSet ds = new DataSet();
ds.ReadXml(src, System.Data.XmlReadMode.Auto);
string strOldTbName = ds.Tables[0].TableName;
ds.Tables[0].TableName = "Loan";
p_szXml = ds.GetXml();
//end
int i = 0, j = 0;
while ( i < j =" i+1;" bldup =" true;" k =" 0" dstmp =" new" p_szxml =" (new" src =" new" ds =" new" tablename =" strOldTbName;" p_szxml =" ds.GetXml();">
/// Remove the duplicated node with the specified loan no in the xml
///
///
///
///
private string RemoveDuplicatedLoanById(string p_szXml, string p_szLoanNo)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(p_szXml);
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("descendant::Loan[loan_no='" + p_szLoanNo + "']");
if (nodeList == null)
return p_szXml;
for (int i = 1 ; i < nodeloan =" nodeList[i];" xdoc =" new" nodes =" XDoc.SelectNodes(path);" n =" Nodes.Count;" m =" tag_names.GetLength(0);" i =" 0;" j =" 0;" node =" Nodes.Item(i).SelectSingleNode(tag_names[j]);" innertext =" Node.InnerText.Replace(" szresult =" TagValue;" szresult =" szResult.Replace(" szresult =" szResult.Replace(" szresult =" szResult.Replace(">", ">");
szResult = szResult.Replace("\'", "'");
szResult = szResult.Replace("\"", """);
return szResult;
}
public static string BuildTag(string TagName, string TagValue)
{
if (TagValue != "")
{
return "<" + TagName + ">" + TagValue + "}
else
{
return "";
}
} //BuildTag
public static string BuildChekedTag(string TagName, string TagValue)
{
if (TagValue != "")
{
TagValue = ReplaceSpecialChars(TagValue);
return "<" + TagName + ">" + TagValue + "}
else
{
return "";
}
} //BuildTag
public static string GetNode(string xml_data, string node_name) //DM 01.02.03
{
return "<" + node_name + ">" + GetNodeValue(xml_data, node_name) + "} //GetNode
public static string GetNodeValue(string xml_data, string node_name) //DM 10/25/02
{
int i, j;
string data = "";
i = xml_data.IndexOf("<" + node_name + ">");
if (i >= 0)
{
i = i + node_name.Length + 2;
j = xml_data.LastIndexOf("data = xml_data.Substring(i, j-i);
}
return data;
} //GetNodeValue
}
}

Không có nhận xét nào: