0
<form id="form1" runat="server">
<div>
    <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All" ShowLines="true" ShowExpandCollapse="true">

<Nodes>
<asp:TreeNode Value="Child1" Expanded="True" Text="Server1">
  <asp:TreeNode Value="Grandchild1" Text="MBS1" />
  <asp:TreeNode Value="Grandchild2" Text="MBS2" />
</asp:TreeNode>
<asp:TreeNode Value="Child2" Text="Server2" />
<asp:TreeNode Value="Child3" Expanded="True" Text="MBS3">
  <asp:TreeNode Value="Grandchild1" Text="MBS4" />
</asp:TreeNode>
 </Nodes>
</asp:TreeView>
</div>
<p>
    <asp:Button ID="btnCreateXML" runat="server" onclick="btnCreateXML_Click" 
        Text="Create XML" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</p>
</form>

代码背后:

using System.Xml;

 public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
    TreeView1.Attributes.Add("onclick", "javascript: OnTreeClick();");
}
protected void btnCreateXML_Click(object sender, EventArgs e)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(path);
    XmlNode Parent = doc.CreateElement("Parent");
    XmlNode Child = doc.CreateElement("Child");
    doc.Save(path);
    string temp;
    char[] al = new char[5];
    foreach (TreeNode node in TreeView1.CheckedNodes)
    {
        //temp = (node.Text).ToString();
        Child.InnerText = node.Text;
        Parent.AppendChild(Child);

        Response.Write(node.Text);
       // al[node] += al[node];

    }        
 }
}

我确实有 asp.net TreeView 控件,当我检查节点时,我可以从 btnCreateXML 的单击事件后面的代码中获取这些检查值。我将进入 node.Text 的值,我想为它创建 XML 文件。如何实现这一点(仅适用于选定的节点)?任何帮助都会很棒!

4

1 回答 1

0

.aspx 文件:

<asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All" ShowLines="True" ExpandDepth="1">
     <Nodes>
     <asp:TreeNode Text="Entire">
          <asp:TreeNode Text="VM">
            <asp:TreeNode Text="MBS1">   
        </asp:TreeNode>
        <asp:TreeNode Text="PF1"></asp:TreeNode>
        </asp:TreeNode>


        <asp:TreeNode Text="VM2">
        <asp:TreeNode Text="MBS2"></asp:TreeNode>
        <asp:TreeNode Text="PF2"></asp:TreeNode>
        </asp:TreeNode>

    </asp:TreeNode>
    </Nodes>
    </asp:TreeView>

后面的代码:

protected void btnCreateXML_Click(object sender, EventArgs e) { //Start writer XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/XMLFiles/" + "XMLFile" + ".xml"), System.Text.Encoding.UTF8) ; //启动 XM 文档 writer.WriteStartDocument(true); writer.Formatting = Formatting.Indented; writer.Indentation = 2; //根元素 writer.WriteStartElement("MBServerList"); 字符串温度;writer.WriteStartElement("SelectedNodedsInfo"); foreach(TreeView1.CheckedNodes 中的 TreeNode 节点){ temp = (node.Text).ToString();

        if (node.Checked)
        {     
           if (node.Depth == 0)
                {
                    writer.WriteStartElement("Scanning Entire Tree");
                    writer.WriteString(node.Text);
                    writer.WriteEndElement();
                    break;   
                 }

            if (node.ChildNodes.Count > 0)
            {


              {
                    writer.WriteStartElement("Server");
                    writer.WriteString(node.Text);


                    foreach (TreeNode cnode in node.ChildNodes)
                    {

                        if (cnode.Checked)
                        {
                            writer.WriteStartElement("Store");
                            writer.WriteString(cnode.Text);
                            writer.WriteEndElement();
                        }
                    }
                    writer.WriteEndElement();
                }
            }
            else
            {
                if ((node.Depth == 1)||(node.Depth == 2) && (node.Parent.Checked == false))
                {
                    writer.WriteStartElement("Server");

                    writer.WriteString(node.Parent.Text);

                    writer.WriteStartElement("Store");
                    writer.WriteString(node.Text);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }

        }


    }
    writer.WriteEndElement();
    lblResult.Text = "XML File ceated!";


    writer.WriteEndElement();
    //End XML Document
    writer.WriteEndDocument();
    //Close writer
    writer.Close();
}

更新:从树视图中获取检查值(直到深度级别 3),并为其创建 xml 文件。

于 2012-11-20T12:08:07.867 回答