树视图节点的XML重新定位

时间:2018-01-22 07:38:15

标签: c# xml treeview

我正在尝试从XML文件导入并导出到treeview。但我有这个问题。 sample treeview具有一个根节点和两个相同父节点的子节点。我用于导出和导入的代码不起作用。当我导出树视图时,保存的XML文件是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<AAA>
BBBCCC</AAA>

当我将该xml文件导入treeview时,它看起来像after import xml to treeview。我用于出口和进口的代码是:

//Open the XML file, and start to populate the treeview
private void populateTreeview()
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Title = "Open XML Document";
    dlg.Filter = "XML Files (*.xml)|*.xml";
    dlg.FileName = Application.StartupPath + "\\..\\..\\example.xml";
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        try
        {
            //Just a good practice -- change the cursor to a 
            //wait cursor while the nodes populate
            this.Cursor = Cursors.WaitCursor;
            //First, we'll load the Xml document
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(dlg.FileName);        
            //Now, clear out the treeview, 
            //and add the first (root) node
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add(new 
              TreeNode(xDoc.DocumentElement.Name));
            TreeNode tNode = new TreeNode();
            tNode = (TreeNode)treeView1.Nodes[0];
            //We make a call to addTreeNode, 
            //where we'll add all of our nodes
            addTreeNode(xDoc.DocumentElement, tNode);
            //Expand the treeview to show all nodes
            treeView1.ExpandAll();    
        }
        catch(XmlException xExc) 
          //Exception is thrown is there is an error in the Xml
        {
            MessageBox.Show(xExc.Message);
        }
        catch(Exception ex) //General exception
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            this.Cursor = Cursors.Default; //Change the cursor back
        }
    }
}
//This function is called recursively until all nodes are loaded
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
    XmlNode xNode;
    TreeNode tNode;
    XmlNodeList xNodeList;
    if (xmlNode.HasChildNodes) //The current node has children
    {
        xNodeList = xmlNode.ChildNodes;
        for(int x=0; x<=xNodeList.Count-1; x++) 
          //Loop through the child nodes
        {
            xNode = xmlNode.ChildNodes[x];
            treeNode.Nodes.Add(new TreeNode(xNode.Name));
            tNode = treeNode.Nodes[x];
            addTreeNode(xNode, tNode);
        }
    }
    else //No children, so add the outer xml (trimming off whitespace)
        treeNode.Text = xmlNode.OuterXml.Trim();
}






private XmlTextWriter xr;

public void exportToXml2(TreeView tv, string filename) 
{
    xr = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
    xr.WriteStartDocument();
    //Write our root node
    xr.WriteStartElement(treeView1.Nodes[0].Text);
    foreach (TreeNode node in tv.Nodes)
    {
        saveNode2(node.Nodes);
    }
    //Close the root node
    xr.WriteEndElement();
    xr.Close();
}

private void saveNode2(TreeNodeCollection tnc)
{
    foreach (TreeNode node in tnc)
    {
        //If we have child nodes, we'll write 
        //a parent node, then iterrate through
        //the children
        if (node.Nodes.Count > 0)
        {
            xr.WriteStartElement(node.Text);
            saveNode2(node.Nodes);
            xr.WriteEndElement();    
        } 
        else //No child nodes, so we just write the text
        {
            xr.WriteString(node.Text);
        }
    }
}

问题出在哪里?

3 个答案:

答案 0 :(得分:0)

您使用的语言不是英语。请转换并保存。

var text = File.ReadAllText(file, Encoding.GetEncoding(codePage));

how-to-read-text-files-with-ansi-encoding-and-non-english-letters

File.WriteAllText(filepath, filetext, Encoding.Unicode);

 new System.IO.StreamWriter(new FileStream(file_name,FileMode.OpenOrCreate),Encoding.ASCII);

read-write-text-file-ansi-utf8-unicode

how-to-convert-utf7-string-into-ansi-in-csharp

答案 1 :(得分:0)

代码很好。 xml应该如下所示

<?xml version="1.0" encoding="utf-8" ?>
<AAA>
   <BBB/>
   <CCC/>
</AAA>

答案 2 :(得分:0)

好吧,我修改了几行,然后管理了我想要的内容。

首先,我为特殊字符添加char数组。并将它们用于trim()函数。

 private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
        {
            **char[] karakter = new char[] { '<', '>', '/' };**
            XmlNode xNode;
            TreeNode tNode;
            XmlNodeList xNodeList;
            if (xmlNode.HasChildNodes) //The current node has children
            {
                xNodeList = xmlNode.ChildNodes;
                for (int x = 0; x <= xNodeList.Count - 1; x++)
                {
                    xNode = xmlNode.ChildNodes[x];
                    treeNode.Nodes.Add(new TreeNode(xNode.Name));
                    tNode = treeNode.Nodes[x];
                    addTreeNode(xNode, tNode);
                }
            }
            else
            {
                treeNode.Text = xmlNode.OuterXml.Trim(**karakter**);
            }

其次,如果没有子节点,我使用WriteRaw方法而不是WriteString方法。因为特殊字符被WriteString方法替换。

private void saveNode2(TreeNodeCollection tnc)
{
    foreach (TreeNode node in tnc)
    {
        if (node.Nodes.Count > 0)
        {
            xr.WriteStartElement(node.Text);
            saveNode2(node.Nodes);
            xr.WriteEndElement();
        }
        else 
        {
            **xr.WriteRaw($"<{node.Text}/>");**
        }
    }

但是当我尝试用两个根保存treeview时,它不会写最后一个根。任何意见?最后一个根的孩子被写为第一根的孩子。其他那些代码可以工作。