欢迎光临
我们一直在努力

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

namespace xmlhelper
{
using system;
using system.xml;
using system.xml.xpath;
using system.collections;
using system.net;
using system.io;
using system.text;
using system.text.regularexpressions;
using system.data;

/// <summary>
/// this class attempts to wrap up some common things
/// we need to do when dealing with xml and c# classes:
/// load, save, add/remove attributes/elements, et al.
/// </summary>
public class xmlhelper
{
  private xmldocument m_xmldocument;
  private xpathnavigator m_nav;
  private string m_slasterrormessage;
  public enum loadtype { fromstring, fromlocalfile, fromurl }
  // constructor
  public xmlhelper()
  {
   m_slasterrormessage = "";
   m_xmldocument = new xmldocument();
  }
  // properties…
  public string lasterrormessage
  {
   get { return m_slasterrormessage; }
   set { m_slasterrormessage = value; }
  }
  
  public xmlnode rootnode
  {
   get { return m_xmldocument.documentelement; }
  }
  
  public xmldocument document
  {
   get { return m_xmldocument; }   
  }

  public xpathnavigator navigator
  {
   get { return m_nav; }   
  }
  // delegates – more complex save operations can do it themselves…
  public delegate bool save(string stargetxml);
  
  /// <summary>
  /// save the xml to a target file.
  /// </summary>
  public bool savetofile(string stargetfilename)
  {
   bool bresult = false;
   
   try
   {
    m_xmldocument.save(stargetfilename);
    bresult = true;
   }
   catch (xmlexception e)
   {
    handleexception ( e );
   }
  
   return bresult;
  }
  
  /// <summary>
  /// easy way to get the entire xml string
  /// </summary>
  public override string tostring()
  {
   return m_xmldocument.outerxml;
  }

  private void dopostloadcreateinit()
  {
   m_nav = m_xmldocument.createnavigator();
   movetoroot();
  }

  /// <summary>
  /// easy way to load xml from a file or url
  /// </summary>
  public bool loadxml(string sourcexmlorfile, loadtype loadtype)
  {
   bool bloadresult = false;
   
   try
   {
    switch (loadtype)
    {
     case xmlhelper.loadtype.fromstring:
      m_xmldocument.loadxml(sourcexmlorfile); // loading from source xml text
      break;
     case xmlhelper.loadtype.fromlocalfile:
      m_xmldocument.load(sourcexmlorfile); // loading from a file
      break;
  
     case xmlhelper.loadtype.fromurl:
     {
      string surlcontent = geturlcontent(sourcexmlorfile);
      m_xmldocument.loadxml(surlcontent);
      break;
     }
    
     default:
      string serr = "developer note:  no loadtype case supported for " + loadtype.tostring();
      throw(new exception(serr));
    }
    dopostloadcreateinit();
             
    bloadresult = true;
   }            
   catch (exception e)
   {
    handleexception ( e );
   }
   return bloadresult;
  }   
   
  /// <summary>
  /// helper method to get string content from a url – not necessarily xml, but probably
  /// </summary>
  public string geturlcontent(string surl)
  {
   string s = "";
   try
   {   
    httpwebrequest webreq = (httpwebrequest)webrequest.create(surl);
    httpwebresponse webresp = (httpwebresponse)webreq.getresponse();
    
    streamreader stream = new streamreader(webresp.getresponsestream(), encoding.ascii);
    s = stream.readtoend();
    stream.close();
   }
   catch(exception e)
   {
    handleexception ( e );
   }    
    
   return s;    
  }
  /// <summary>
  /// helper function if navigation is used to ensure were at the root node.
  /// </summary>
  public bool movetoroot()
  {
   bool bresult = false;
   try
   {
    m_nav.movetoroot(); // go to root node!
    bresult = true;
   }
   catch (exception e)
   {
    handleexception ( e );    
   }
   return bresult;
  }
   
  /// <summary>
  /// gets an arraylist of xmlnode children using an xpath expression
  /// </summary>
  public arraylist getchildnodesfromcriteria(string xpathexpression)
  {
   arraylist al = new arraylist();
   try
   {
    xmlnodelist nl = m_xmldocument.selectnodes(xpathexpression);
   
    if ( nl != null )
    {
     for ( int i=0; i<nl.count; i++ )
      al.add (nl.item(i));
    }    
   }
   catch (exception e)
   {
    handleexception ( e );      
   }
     
   return al;
  }
  /// <summary>
  /// get first child node given an xpath expression
  /// </summary>
  public xmlnode getfirstchildnodefromcriteria (string xpathexpression)
  {
   
   xmlnode node = null;
   
   try
   {
    node = m_xmldocument.selectsinglenode(xpathexpression);
   }
   catch (exception e)
   {
    handleexception ( e );      
   }
   
   return node;
  }
      
  /// <summary>
  /// get the attribute value from a given xmlnode
  /// </summary>
  public string getattributevalue(xmlnode node, string sattributename)
  {
   string sval = "";
   try
   {
    xmlattributecollection attribcoll = node.attributes;
    xmlattribute attrib = attribcoll[sattributename, ""];
    sval = decode(attrib.value);
   }       
   catch (exception e)
   {
    handleexception ( e );
   }
    
   return sval;
  }
  /// <summary>
  /// get the attribute int32 (int) value from a given xmlnode
  /// </summary>
  public int getattributeint32value(xmlnode node, string sattributename)
  {
   string sval = getattributevalue ( node, sattributename);
    
   return sval != "" ? convert.toint32(sval) : 0;
  }
  /// <summary>
  /// get the attribute floating point/single value from a given xmlnode
  /// </summary>
  public float getattributefloatvalue(xmlnode node, string sattributename)
  {
   string sval = getattributevalue ( node, sattributename);
   return sval != "" ? convert.tosingle(sval) : 0;
  }
  /// <summary>
  /// get the attribute double value from a given xmlnode
  /// </summary>
  public double getattributedoublevalue(xmlnode node, string sattributename)
  {
   string sval = getattributevalue ( node, sattributename);
   return sval != "" ? convert.todouble(sval) : 0.00;
  }
  /// <summary>
  /// get the attribute boolean value from a given xmlnode
  /// </summary>
  public bool getattributebooleanvalue(xmlnode node, string sattributename)
  {
   string sval = getattributevalue ( node, sattributename);
   return sval != "" ? convert.toboolean(sval) : false;
  }
  /// <summary>
  /// get the element value from a given xmlnode
  /// </summary>
  public string getelementvalue(xmlnode xmlnode)
  {
   string sval = "";
   
   try
   {
    sval = decode(xmlnode.innerxml);
   }       
   catch (exception e)
   {
    handleexception ( e );
   }
   return sval;
  }
  /// <summary>
  /// get the element int32 value from a given xmlnode
  /// </summary>
  public int getelementint32value(xmlnode xmlnode)
  {
   string sval = getelementvalue (xmlnode);
   return sval != "" ? convert.toint32(sval) : 0;
  }
  /// <summary>
  /// get the element float/single floating point value from a given xmlnode
  /// </summary>
  public float getelementfloatvalue(xmlnode xmlnode)
  {
   string sval = getelementvalue (xmlnode);
   return sval != "" ? convert.tosingle(sval) : 0;
  }
  /// <summary>
  /// get the element double value from a given xmlnode
  /// </summary>
  public double getelementdoublevalue(xmlnode xmlnode)
  {
   string sval = getelementvalue (xmlnode);
   return sval != "" ? convert.todouble(sval) : 0.00;
  }
  /// <summary>
  /// get the element boolean value from a given xmlnode
  /// </summary>
  public bool getelementbooleanvalue(xmlnode xmlnode)
  {
   string sval = getelementvalue (xmlnode);
   return sval != "" ? convert.toboolean(sval) : false;
  }
  /// <summary>
  /// get the first child element value from a given xmlnode
  /// </summary>
  public string getchildelementvalue(xmlnode parentnode, string selementname)
  {
   string sval = "";
   try
   {
    xmlnodelist childnodes = parentnode.childnodes;
    foreach (xmlnode childnode in childnodes)
    {
     if (childnode.name == selementname)
     {
      sval = getelementvalue ( childnode );
      break;  
     }
    }
   }       
   catch (exception e)
   {
    handleexception ( e );
   }
   return sval;
  }
  /// <summary>
  /// get the child element int32 value from a given xmlnode and elementname
  /// </summary>
  public int getchildelementint32value(xmlnode parentnode, string selementname)
  {
   string sval = getchildelementvalue (parentnode, selementname);
   return sval != "" ? convert.toint32(sval) : 0;
  }
  /// <summary>
  /// get the child element floating point/single value from a given xmlnode and elementname
  /// </summary>
  public float getchildelementfloatvalue(xmlnode parentnode, string selementname)
  {
   string sval = getchildelementvalue (parentnode, selementname);
   return sval != "" ? convert.tosingle(sval) : 0;
  }
  /// <summary>
  /// get the child element double value from a given xmlnode and elementname
  /// </summary>
  public double getchildelementdoublevalue(xmlnode parentnode, string selementname)
  {
   string sval = getchildelementvalue (parentnode, selementname);
   return sval != "" ? convert.todouble(sval) : 0.00;
  }
  /// <summary>
  /// get the child element boolean value from a given xmlnode and elementname
  /// </summary>
  public bool getchildelementbooleanvalue(xmlnode parentnode, string selementname)
  {
   string sval = getchildelementvalue (parentnode, selementname);
   return sval != "" ? convert.toboolean(sval) : false;
  }
  /// <summary>
  /// returns the first xmlnode object matching this element name
  /// <seealso cref=getfirstchildxmlnode/>
  /// </summary>
  public xmlnode getfirstchildxmlnodefromroot(string selementname)
  {
   // todo:  isnt there a better/faster/more effiecient way to do this?  couldnt find it sifting through documentation!
   xmlnodelist nodelist = getchildnodesfromroot(selementname);
   if (nodelist.count > 0)
    return nodelist[0];
   
   return null;
  }

  /// <summary>
  /// returns the first xmlnode object matching this element name
  /// note:  this doesnt seem to work if parent is root!  use getfirstchildxmlnodefromroot
  /// <seealso cref=getfirstchildxmlnodefromroot/>
  /// </summary>
  public xmlnode getfirstchildxmlnode(xmlnode parentnode, string selementname)
  {
   // note:  this doesnt seem to work if parent is root!  use getfirstchildxmlnodefromroot
   xmlnode foundchildnode = null;
   try
   {
    xmlnodelist childnodes = parentnode.childnodes;
    foreach (xmlnode childnode in childnodes)
    {
     if (childnode.name == selementname)
     {
      foundchildnode = childnode;
      break;  
     }
    }
   }       
   catch (exception e)
   {
    handleexception ( e );
   }
   return foundchildnode;
  }
  /// <summary>
  /// returns an xmlnodelist of child nodes matching this element name
  /// </summary>
  public xmlnodelist getchildnodesfromroot(string selementname)
  {
   return m_xmldocument.getelementsbytagname(selementname);
  }
  
  /// <summary>
  /// returns an arraylist (boxed xmlnode objects) of child nodes matching this element name
  /// this function is recursive in that it will find all the children, even if their in
  /// sub folders (sub child nodes)
  /// </summary>
  public arraylist getrecursivechildnodesfromparent(xmlnode parentnode, string selementname)
  {
   arraylist elementlist = new arraylist();
    
   try
   {
    xmlnodelist children = parentnode.childnodes;
    foreach (xmlnode child in children)
    {
     if (child.name == selementname)
      elementlist.add(child);
     
     if (child.haschildnodes == true)
     {
      arraylist childrenlist = getrecursivechildnodesfromparent(child, selementname);
      if (childrenlist.count > 0)
      {
       foreach (xmlnode subchild in childrenlist)
        elementlist.add(subchild);
      }
     }
    }
   }
   catch (exception e)
   {
    handleexception ( e );
   }
       
   return elementlist;
  }
  
   

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

相关推荐

  • 暂无文章