假如有以下的XML文件<?xml version="1.0" encoding="UTF-8"?>
<contacts>
<contact>
<name>张小三</name>
<sex>男</sex>
<mobile>13899220203</mobile>
</contact>
<contact>
<name>王晓光</name>
<sex>男</sex>
<mobile>13919281133</mobile>
</contact>
<contact>
<name>李小明</name>
<sex>男</sex>
<mobile>13800818111</mobile>
</contact>
</contacts>
怎么在一个TreeView或是一个TextBox多行的显示...格式:contacts
 contact
  name
   第一个人的名字
  /name
  sex
   性别
  /sex
  mobile
   手机号码
  /mobile
 /contact contact
  name
   第二个人的名字
  /name
  sex
   性别
  /sex
  mobile
   手机号码
  /mobile
 /contact contact
  name
   第三个人的名字
  /name
  sex
   性别
  /sex
  mobile
   手机号码
  /mobile
 /contact
/contacts
 

解决方案 »

  1.   

    XmlDocument _xml = new XmlDodument();
    _xml.Load(文件路径);
    XmlNodeList _nodeList = _xml.SelectNodes("contact");
    foreach(XmlNode _node in _nodeList )
    {
    TextBox1.Text = _node.ChildNodes.Item(1).Value;
    }
    大概这样
      

  2.   

    Visual C#中使用XML之实现DOM  
     http://dev.yesky.com/msdn/319/2502319.shtml    C#中操作xml文件(插入节点、修改、删除) 
    http://www.qqread.com/csharp/p359862.html 
    C#中操作XML修改完整版 
    http://www.itwis.com/html/net/c/20080219/954.html 
    Manipulate XML data with XPath and XmlDocument (C#) 
    http://www.codeproject.com/KB/cpp/myXPath.aspx 
    使用 Visual C# .NET 通过 XPath 表达式查询 XML 
    http://support.microsoft.com/kb/308333/zh-cn 
    ASP.NET 2.0 中的数据访问--XmlDataSource 
    http://www.itkm.cn/article.asp?id=225 
    使用XMLDataSource简单实现多级下拉菜单 
    http://blog.csdn.net/cui2nd/archive/2005/12/20/557315.aspx 
    XML技术讲座之六 XML中的命名空间 
    http://cnw2005.cnw.com.cn/issues/2000/49/4914b.asp 
    C#操作xml之xpath语法 
    http://www.cnblogs.com/amboyna/archive/2007/12/18/1003852.html 
    XML文件操作指南 
    http://www.cnblogs.com/jetz/archive/2005/07/30/203831.html
      

  3.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowImage.aspx.cs" Inherits="Xml_ShowImage" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="text-align: center">
            <asp:Label ID="Label1" runat="server" Text="利用XML传送数据" Width="196px"></asp:Label><br />
            <br />
            <asp:Label ID="Label2" runat="server" Text="上传的图片" Width="94px"></asp:Label>
            <input id="File1" runat="server" type="file" /><br />
            <br />
            <asp:Label ID="Label3" runat="server" Text="描述"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" Height="71px" TextMode="MultiLine" Width="280px"></asp:TextBox><br />
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="保存" /></div>
        </form>
    </body>
    </html>
       protected void Button1_Click(object sender, EventArgs e)
        {
            string str = File1.PostedFile.FileName;
            string filename = Path.GetFileName(str);
            int fileLength = File1.PostedFile.ContentLength;        if (fileLength < 0)
                return;
            Byte[] ByteImage = new byte[fileLength];
            Stream ObjectStream = File1.PostedFile.InputStream;        ObjectStream.Read(ByteImage, 0, fileLength);        string strfilename = Server.MapPath(".\\Db\\image.xml");        XmlDocument doc = new XmlDocument();
            doc.Load(strfilename);
            XmlNode nodelist = doc.SelectSingleNode("ImageMes");
            //XmlNodeList node = doc.SelectSingleNode("imageMes").ChildNodes;        XmlElement xe = doc.CreateElement("Image");
            XmlElement xe1 = doc.CreateElement("ImageType");
            xe1.InnerText = File1.PostedFile.ContentType;
            xe.AppendChild(xe1);        XmlElement xe2 = doc.CreateElement("ImageSize");
            xe2.InnerText = File1.PostedFile.ContentLength;
            xe.AppendChild(xe2);        XmlElement xe3 = doc.CreateElement("ImageDescription");
            xe3.InnerText = TextBox1.Text;
            xe.AppendChild(xe3);        XmlElement xe4 = doc.CreateElement("ImageData");
            xe4.InnerText = Convert.ToBase64String(ByteImage);
            xe.AppendChild(xe4);        nodelist.AppendChild(xe);
            doc.Save(Server.MapPath(".\\Db\\image.xml"));
        }
      

  4.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddNews.aspx.cs" Inherits="News_AddNews" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <link href="../Css/StyleSheet.css" rel="stylesheet" type="text/css" />  
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="text-align: center">
            <table border="0" cellpadding="0" cellspacing="0" style="width: 651px; height: 181px">
                <tr>
                    <td colspan="2">
                        <asp:Label ID="Label1" runat="server" Text="添加新闻" Width="96px" Height="22px"></asp:Label></td>
                </tr>
                <tr>
                    <td style="width: 100px; height: 9px; text-align: center;">
                        <asp:Label ID="Label2" runat="server" Text="新闻标题" Width="94px"></asp:Label></td>
                    <td style="width: 203px; height: 9px;">
                        <asp:TextBox ID="TextBox1" runat="server" Width="299px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 100px; height: 19px; text-align: center;">
                        <asp:Label ID="Label5" runat="server" Text="新闻类别" Width="68px"></asp:Label></td>
                    <td style="width: 203px; height: 19px; text-align: left;">
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 100px; height: 19px; text-align: center">
                        <asp:Label ID="Label3" runat="server" Text="作者" Width="94px"></asp:Label></td>
                    <td style="width: 203px; height: 19px; text-align: left">
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 100px; text-align: center;">
                        <asp:Label ID="Label4" runat="server" Text="内容" Width="90px"></asp:Label></td>
                    <td style="width: 203px">
                        <asp:TextBox ID="TextBox3" runat="server" Height="174px" TextMode="MultiLine" Width="464px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" /></td>
                </tr>
            </table>
        
        </div>
        </form>
    </body>
    </html> protected void Button1_Click(object sender, EventArgs e)
        {
            string title = TextBox1.Text;
            string type = TextBox4.Text;
            string author = TextBox2.Text;
            string content = TextBox3.Text;
            string datetime = DateTime.Now.ToString();
            //首先加载XML
            string strxml = Server.MapPath("../News/Xml/XMLFile.xml");
            XmlDocument Xmldoc = new XmlDocument();
            Xmldoc.Load(strxml);
            string id;
            //XML的根节点 
            XmlNode xn = Xmldoc.SelectSingleNode("Mes");
            XmlElement xe = Xmldoc.CreateElement("News");
            if (xn.HasChildNodes)
            {
                id = Convert.ToString(Convert.ToInt32(xn.LastChild.FirstChild.InnerText) + 1);
            }
            else
            {
                id = "1";
            }
            //添加新闻ID
            XmlElement xid = Xmldoc.CreateElement("NewsID");
            xid.InnerText = id;
            xe.AppendChild(xid);
            //添加新闻类别
            XmlElement xtype = Xmldoc.CreateElement("NewsType");
            xtype.InnerText = type;
            xe.AppendChild(xtype);
          
            //添加新闻作者
            XmlElement xauthor =Xmldoc.CreateElement("NewsAuthor");
            xauthor.InnerText = author;
            xe.AppendChild(xauthor);
            //添加新闻标题
            XmlElement xtitle = Xmldoc.CreateElement("NewsTitle");
            xtitle.InnerText = title;
            xe.AppendChild(xtitle);
            //添加新闻内容
            XmlElement xcontent =Xmldoc.CreateElement("NewsContent");
            xcontent.InnerText = content;
            xe.AppendChild(xcontent);        //添加新闻时间
            XmlElement xdate = Xmldoc.CreateElement("NewsDate");
            xdate.InnerText = datetime;
            xe.AppendChild(xdate);        xn.AppendChild(xe);
            Xmldoc.Save(strxml);
            Response.Write("<script language='javascript'>alert('添加成功');</script>");
         
        }
      

  5.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="NewsEdit.aspx.cs" Inherits="News_NewsEdit" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <link href="../Css/StyleSheet.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript">
         function Init()
        {
          var vv="中国";
          var ss='<%=ShowMes("vv") %>';
          alert(ss);
        }    </script>
    </head>
    <body style="text-align: center">
        <form id="form1" runat="server">
         <table border="0" cellpadding="0" cellspacing="0" style="width: 651px; height: 181px">
                <tr>
                    <td colspan="2">
                        <asp:Label ID="Label1" runat="server" Text="修改新闻" Width="96px" Height="22px"></asp:Label></td>
                </tr>
                <tr>
                    <td style="width: 100px; height: 9px; text-align: center;">
                        <asp:Label ID="Label2" runat="server" Text="新闻标题" Width="94px"></asp:Label></td>
                    <td style="width: 203px; height: 9px;">
                        <asp:TextBox ID="TextBox1" runat="server" Width="299px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 100px; height: 19px; text-align: center;">
                        <asp:Label ID="Label5" runat="server" Text="新闻类别" Width="68px"></asp:Label></td>
                    <td style="width: 203px; height: 19px; text-align: left;">
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 100px; height: 19px; text-align: center">
                        <asp:Label ID="Label3" runat="server" Text="作者" Width="94px"></asp:Label></td>
                    <td style="width: 203px; height: 19px; text-align: left">
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 100px; text-align: center;">
                        <asp:Label ID="Label4" runat="server" Text="内容" Width="90px"></asp:Label></td>
                    <td style="width: 203px">
                        <asp:TextBox ID="TextBox3" runat="server" Height="174px" TextMode="MultiLine" Width="464px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" /></td>
                </tr>
            </table>
        
        </form>
    </body>
    </html> protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string strurl = Server.MapPath("../News/Xml/XMLFile.xml");
                string id = Request.QueryString["id"];
                XmlDocument doc = new XmlDocument();
                doc.Load(strurl);
                XmlNode node = doc.SelectSingleNode("Mes/News[NewsID='" + id + "']");
                if (node.HasChildNodes)
                {
                    //类别,作者,标题
                    TextBox1.Text = node.ChildNodes[3].InnerText;
                    TextBox2.Text = node.ChildNodes[2].InnerText;
                    TextBox3.Text = node.ChildNodes[4].InnerText;
                    TextBox4.Text = node.ChildNodes[1].InnerText;
                }
               
            }    }
        //public void Page_Init(object sender, EventArgs e)
        //{
        //    ShowMes("aa");
        //}
        public string ShowMes(string str)
        {
            string ss = "你好" + str;
            return ss;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string strurl = Server.MapPath("../News/Xml/XMLFile.xml");
            string id = Request.QueryString["id"];
            string title = Request.Form["TextBox1"];
            string type = Request.Form["TextBox4"];
            string author = Request.Form["TextBox2"];
            string content = Request.Form["TextBox3"];
            XmlDocument doc = new XmlDocument();
            doc.Load(strurl);
            XmlNode xnode = doc.SelectSingleNode("Mes");
            XmlNodeList nodelist = xnode.ChildNodes;
            for(int i=0;i<nodelist.Count;i++)
            {
                XmlNodeList xm = nodelist.Item(i).SelectNodes("NewsID");
                XmlElement xn = (XmlElement)xm.Item(0);
                if (xn.InnerText == id)
                {
                    xn.ParentNode.ChildNodes.Item(1).InnerText = type;
                    xn.ParentNode.ChildNodes.Item(2).InnerText= author;
                    xn.ParentNode.ChildNodes.Item(3).InnerText = title;
                    xn.ParentNode.ChildNodes.Item(4).InnerText = content;
                    //xn.ParentNode.ChildNodes["content"] = content;
                }
            }
            doc.Save(strurl);    }LZ还要吗?
      

  6.   

    xe2.InnerText = File1.PostedFile.ContentLength;这句有错:F:\study_online\WebForm2.aspx.cs(81): 无法将类型“int”隐式转换为“string”我只想简单易懂的...你们注释也没个...真的没法弄下去...我是菜鸟...