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

XMLHelp下

2007-07-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介://接上回 /// /// Create an Element under the given parent based on the name and value pair. /// public XMLElement CreateNodeElement(XMLNode parentNode, string sElementName, string sElementValue)...
关键字: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
相关文章