XMLHelp 下
发表于:2007-06-30来源:作者:点击数:
标签:
//接上回 /// summary /// Create an Element under the given parent based on the name and value pair. /// /summary public XmlElement CreateNodeElement(XmlNode parentNode, string sElementName, string sElementValue) { XmlElement newElem = null;
//接上回
/// <summary>
/// Create an Element under the given parent based on the name and value pair.
/// </summary>
public XmlElement CreateNodeElement(XmlNode parentNode, string sElementName, string sElementValue)
{
XmlElement newElem = null;
try
{
newElem = m_xmlDocument.CreateElement(sElementName);
newElem.InnerXml = Encode(sElementValue);
XmlDocument ownerDoc = parentNode.OwnerDocument;
if (ownerDoc != null)
{
parentNode.AppendChild(newElem);
}
else
{
XmlElement root = m_xmlDocument.DocumentElement;
root.AppendChild(newElem);
}
}
catch (Exception e)
{
HandleException ( e );
}
return newElem;
}
/// <summary>
/// Creates and adds a comment before the given node. If root node, or null,
/// the comment node is Appended to the tree.
/// </summary>
public XmlNode CreateComment(XmlNode insertAfterThisNode, string sVal)
{
if ( insertAfterThisNode == null )
return null;
XmlNode createdNode = null;
try
{
XmlComment commentNode = m_xmlDocument.CreateComment(Encode(sVal));
createdNode = insertAfterThisNode.AppendChild(commentNode);
}
catch ( Exception e )
{
HandleException ( e );
}
return createdNode;
}
public XmlNode CreateXmlDeclaration(string version, string encoding, string standalone)
{
XmlNode createdNode = null;
try
{
XmlDeclaration dec = m_xmlDocument.CreateXmlDeclaration(version, encoding, standalone);
createdNode = m_xmlDocument.PrependChild ( dec );
}
catch ( Exception e )
{
HandleException ( e );
}
return createdNode;
}
/// <summary>
/// Delete an XmlNode from the tree
/// </summary>
public bool DeleteNodeElement(XmlNode targetNode)
{
bool bResult = false;
try
{
XmlNode xmlNode = RootNode.RemoveChild(targetNode);
if (xmlNode != null)
bResult = true;
}
catch (Exception e)
{
HandleException ( e );
}
return bResult;
}
/// <summary>
/// Modify an XmlNode elment with a new value.
/// </summary>
public bool ModifyNodeElementValue(XmlNode targetNode, string sNewElementValue)
{
bool bResult = false;
try
{
targetNode.InnerXml = Encode(sNewElementValue);
bResult = true;
}
catch (Exception e)
{
HandleException ( e );
}
return bResult;
}
/// <summary>
/// Create a new attribute given an XmlElement (XmlNode) target
/// </summary>
public XmlAttribute CreateNodeAttribute(XmlElement targetElement, string sAttributeName, string sAttributeValue)
{
XmlAttribute newAttr = null;
try
{
newAttr = m_xmlDocument.CreateAttribute(sAttributeName);
targetElement.SetAttributeNode(newAttr);
targetElement.SetAttribute(sAttributeName, "", Encode(sAttributeValue));
}
catch (Exception e)
{
HandleException ( e );
}
return newAttr;
}
/// <summary>
/// Delete an attribute from the given target node.
/// </summary>
public bool DeleteNodeAttribute(XmlNode targetNode, string sAttributeName)
{
bool bResult = false;
try
{
XmlAttributeCollection attrColl = targetNode.Attributes;
XmlAttribute xmlAttribute = attrColl.Remove((XmlAttribute)attrColl[sAttributeName,""]);
if (xmlAttribute != null)
bResult = true;
}
catch (Exception e)
{
HandleException ( e );
}
return bResult;
}
/// <summary>
/// GenerateSchema a schema file from a given target file
/// </summary>
public bool GenerateSchema(string sTargetFile)
{
bool bResult = false;
try
{
DataSet data = new System.Data.DataSet();
data.ReadXml ( new XmlNodeReader(RootNode), XmlReadMode.Auto);
data.WriteXmlSchema(sTargetFile);
bResult = true;
}
catch (Exception e)
{
HandleException ( e );
}
return bResult;
}
/// <summary>
/// GenerateSchemaAsString based on the currently loaded Xml
/// </summary>
public string GenerateSchemaAsString()
{
string sSchemaXmlString = "";
try
{
DataSet data = new System.Data.DataSet();
data.ReadXml ( new XmlNodeReader(RootNode), XmlReadMode.Auto);
string sTempFile = Path.GetTempFileName();
data.WriteXmlSchema(sTempFile);
// read the data into a string
StreamReader sr = new StreamReader ( sTempFile );
sSchemaXmlString = sr.ReadToEnd();
sr.Close();
if (File.Exists(sTempFile) == true )
File.Delete(sTempFile);
}
catch (Exception e)
{
HandleException ( e );
sSchemaXmlString = "<root><error>" + LastErrorMessage + "</error></root>";
}
return sSchemaXmlString;
}
/// <summary>
/// Modify an attribute value to a new value
/// </summary>
public bool ModifyNodeAttributeValue(XmlNode targetNode, string sAttributeName, string sNewAttributeValue)
{
bool bResult = false;
try
{
XmlAttributeCollection attrColl = targetNode.Attributes;
XmlAttribute xmlAttribute = (XmlAttribute)attrColl[sAttributeName,""];
xmlAttribute.Value = Encode(sNewAttributeValue);
bResult = true;
}
catch (Exception e)
{
HandleException ( e );
}
return bResult;
}
/// <summary>
/// Internal method used to ensure that HTML and XML tags are encoded within their values
/// </summary>
private string Encode(string input)
{
string output = input;
output = Regex.Replace(output, "&", "&");
output = Regex.Replace(output, "<", "<");
output = Regex.Replace(output, ">", ">");
output = Regex.Replace(output, """, """);
return output;
}
/// <summary>
/// Internal method used to ensure that HTML and XML tags are decoded for display in other systems
/// </summary>
private string Decode(string input)
{
string output = input;
output = Regex.Replace(output, "&","&" );
output = Regex.Replace(output, "<", "<" );
output = Regex.Replace(output, ">", ">" );
output = Regex.Replace(output, """, """ );
return output;
}
/// <summary>
/// Internal method used to process errors and exception handling
/// </summary>
private void HandleException (Exception e )
{
m_sLastErrorMessage = e.Message;
Console.WriteLine(m_sLastErrorMessage + " Stack Trace: " + e.StackTrace + " Source: " + e.Source);
}
} // end of XmlHelper class
} // end of XmlHelper namespace
原文转自:http://www.ltesting.net