繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> XML应用 >> XMLHelp 下

XMLHelp 下

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:15  文字大小:【】【】【
简介://接上回 /// <summary>   /// Create an Element under the given parent based on the name and value pair.   /// </summary>   public XmlElement CreateNodeEle...
关键字:XMLHelp

//接上回

///

/// Create an Element under the given parent based on the name and value pair.

///

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;

}

///

/// Creates and adds a comment before the given node. If root node, or null,

/// the comment node is Appended to the tree.

///

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;

}

///

/// Delete an XmlNode from the tree

///

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;

}

///

/// Modify an XmlNode elment with a new value.

///

public bool ModifyNodeElementValue(XmlNode targetNode, string sNewElementValue)

{

bool bResult = false;

try

{

targetNode.InnerXml = Encode(sNewElementValue);

bResult = true;

}

catch (Exception e)

{

HandleException ( e );

}

return bResult;

}

///

/// Create a new attribute given an XmlElement (XmlNode) target

///

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;

}

///

/// Delete an attribute from the given target node.

///

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;

}

///

/// GenerateSchema a schema file from a given target file

///

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;

}

///

/// GenerateSchemaAsString based on the currently loaded Xml

///

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 = "" + LastErrorMessage + "";

}

return sSchemaXmlString;

}

///

/// Modify an attribute value to a new value

///

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;

}

///

/// Internal method used to ensure that HTML and XML tags are encoded within their values

///

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;

}

///

/// Internal method used to ensure that HTML and XML tags are decoded for display in other systems

///

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;

}

///

/// Internal method used to process errors and exception handling

///

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

责任编辑:admin
相关文章