欢迎光临
我们一直在努力

建站超值云服务器,限时71元/月

//接上回
/// <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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 »
分享到: 更多 (0)

相关推荐

  • 暂无文章