当前位置:编程学习 > C#/ASP.NET >>

asp.net 读写 XML

using System.Xml;
//初始化一个xml实例
XmlDocument xml=new XmlDocument();

//导入指定xml文件
xml.Load(path);
xml.Load(HttpContext.Current.Server.MapPath("../7in10.xml"));

//指定一个节点
XmlNode root=xml.SelectSingleNode("/root");

//获取节点下所有直接子节点
XmlNodeList childlist=root.ChildNodes;

//判断该节点下是否有子节点
root.HasChildNodes;

//获取同名同级节点集合
XmlNodeList nodelist=xml.SelectNodes("/Root/News");

//生成一个新节点
XmlElement node=xml.CreateElement("News");

//将节点加到指定节点下,作为其子节点
root.AppendChild(node);

//将节点加到指定节点下某个子节点前
root.InsertBefore(node,root.ChildeNodes[i]);

//为指定节点的新建属性并赋值
node.SetAttribute("id","11111");

//为指定节点添加子节点
root.AppendChild(node);

//获取指定节点的指定属性值
string id=node.Attributes["id"].Value;

//获取指定节点中的文本
string content=node.InnerText;

//保存XML文件
xml.Save(path);
xml.Save(HttpContext.Current.Server.MapPath("../7in10.xml"));  --------------------编程问答-------------------- --------------------编程问答-------------------- try。。。。



/// <summary>
        /// 获得页面的html代码
        /// </summary>
        /// <param name="url">页面地址</param>
        protected string getHtml(string url)
        {
            string html = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "*/*";
            HttpWebResponse response = null;
            Stream stream = null;
            StreamReader reader = null;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                stream = response.GetResponseStream();
                reader = new StreamReader(stream, Encoding.UTF8);
                html = reader.ReadToEnd();
            }
            catch (Exception excpt)
            {
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
            return html;
        }



C#获取HTML源码 --------------------编程问答-------------------- linq to xml --------------------编程问答-------------------- 刚刚搞错啦...不好意思....


XmlDocument xdXML = new XmlDocument(); xdXML.Load("config.xml"); 
C#读取XML文件示例

1、config.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<Address>
  <HttpAddress>http://www.taian-seo.com</HttpAddress>
</Address>

2、
private void LoadConfig()
{
  string xmlPath = Application.StartupPath + "\\config.xml";
  try
  {
     XmlDocument xdXML = new XmlDocument();
     xdXML.Load(xmlPath);
     XmlNode node = xdXML.SelectSingleNode("//HttpAddress");
     if (null != node)
     {
       txtHttpAddress.Text = node.InnerXml.Trim();
     }
     else
     {
       txtHttpAddress.Text = "";
     }
   }catch (FileNotFoundException)
   {
     MessageBox.Show("因缺少config.xml配置文件,而无法找到Web站点的地址!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }
   catch (Exception)
   {
     MessageBox.Show("访问Web站点时地址未找到!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }
}
 

--------------------编程问答-------------------- what are u what to do what what what!!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,