具有适当缩进的Treeview到XML c#

时间:2015-03-16 21:46:18

标签: c# xml treeview indentation

我无法将Treeview导出到具有适当缩进的Xml文件,我查看了论坛但找不到任何内容。

这是一个关于如何将Xml写入Treeview的链接 Recursion, parsing xml file with attributes into treeview c#

以下是我的代码尝试将树视图导出为XML

public void exportToXml(TreeView tv, string filename)
        {
            sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
            //Write the header
            sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            //Write our root node
            sr.WriteLine("<" + treeView1.Nodes[0].Text + ">");
            foreach (TreeNode node in tv.Nodes)
            {
                saveNode(node.Nodes);
            }
            //Close the root node
            sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");
            sr.Close();
        }

        private void saveNode(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)
                {
                    sr.WriteLine("\t\t<" + node.Text + ">");
                    saveNode(node.Nodes);
                    sr.WriteLine("\t\t</" + node.Text + ">");
                }
                else //No child nodes, so we just write the text
                    sr.WriteLine("\t\t\t"+node.Text);    
            }    
        }

Xml输出

<?xml version="1.0" encoding="utf-8" ?>
<DataConfiguration xmln="abcefg12345" xmlns:xsi="12345abcefg" xsi:schemaLocation="12345abcefg12345abcefg">
        <Hosts>
        <Sites>
        <Site Name="ss">
        <Host Id="aa">
            Address Host="www.www.com"
        </Host Id="aa">
        <Host Id="ee">
            Address Host="www.www.com"
        </Host Id="ee">
        <Host Id="dd">
            Address Host="www.www.com"
        </Host Id="dd">
        <Host Id="pp">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com/"
        </Host Id="pp">
        <Host Id="ss">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
        </Host Id="ss">
        <Host Id="561">
            Address Host="www.www.com"
        </Host Id="561">
        </Site Name="ss">
        <Site Name="hihi">
        <Host Id="cc">
            Address Host="www.www.com"
        </Host Id="cc">
        <Host Id="sdD">
            Address Host="www.www.com"
        </Host Id="sdD">
        <Host Id="8uj">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
        </Host Id="8uj">
        <Host Id="222">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
        </Host Id="222">
        <Host Id="hhh">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
        </Host Id="hhh">
        <Host Id="hhh">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
        </Host Id="hhh">
        </Site Name="hihi">
        </Sites>
        <Host Id="hhh">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
        </Host Id="hhh">
        <Host Id="hhh">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
        </Host Id="hhh">
        <Host Id="hhh">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
        </Host Id="hhh">
        <Host Id="hhh">
            Address Scheme="ppp" Host="www.www.com" Path="www.www.com"
        </Host Id="hhh">
        </Hosts>
        <DataPools>
        <DataPool Id="sss" default="sure">
        <DataGroup Id="sss" Parent="aaa" UserCanSelectHost="sure">
        <HostId Parent="hhhh">
            I'm breaking here
        </HostId Parent="hhhh">
        <DataSources>
            empty
        </DataSources>
        </DataGroup Id="sss" Parent="aaa" UserCanSelectHost="sure">
        <DataGroup Id="ccc" UserCanSelectHost="whynot">
        <HostId>
            God I'm breaking here again, i hope you can fix me
        </HostId>
        <DataSources>
            empty
        </DataSources>
        </DataGroup Id="ccc" UserCanSelectHost="whynot">
        <DataGroup Id="sss" UserCanSelectHost="yessure">
        <HostId>
            cry face
        </HostId>
            webfg displaygroup="sss" provider="sss" id="ccc" principal="ccc" nioarc="ccc" nap="ccc" group="ccc"
            nhood port="1234"
        <ServerNames>
            <!-- insert comment -->
            <!-- insert comment -->
            <!-- insert comment -->
        <ServerName>
            myname
        </ServerName>
        <ServerName>
            yourname
        </ServerName>
        </ServerNames>
            <!-- insert comment -->
        <Implementations>
        <Implementation>
        <Name>
            yourname
        </Name>
        <Type>
            typeme
        </Type>
        <Assembly>
            visionme
        </Assembly>
        <Path>
            ohno
        </Path>
        </Implementation>
        </Implementations>
            --&gt;
            cfgman port="ccc"
            webservice provider="ccc"
            webservice provider="ccc"
            webservice provider="ccc"
        <parameters>
            useeventpush value="ccc"
        </parameters>
            webservice provider="ccc"
            pollingFrequency value="1000"
        </DataGroup Id="sss" UserCanSelectHost="yessure">
        </DataPool Id="sss" default="sure">
        <DataGroup Id="ccc " UserCanSelectHost="ccc">
        <DataGroup Id="ccc " UserCanSelectHost="ccc">
        <HostId>
            idk
        </HostId>
        <DataSources>
            empty
        </DataSources>
        </DataGroup Id="ccc " UserCanSelectHost="ccc">
        <DataGroup Id="ccc " UserCanSelectHost="ccc">
        <HostId>
            idk
        </HostId>
        <DataSources>
            empty
        </DataSources>
        </DataGroup Id="ccc " UserCanSelectHost="ccc">
        <DataGroup Id="default" UserCanSelectHost="true">
        <HostId>
            idk
        </HostId>
        </DataGroup Id="default" UserCanSelectHost="true">
        </DataGroup Id="ccc " UserCanSelectHost="ccc">
        </DataPools>
</DataConfiguration xmln="abcefg12345" xmlns:xsi="12345abcefg" xsi:schemaLocation="12345abcefg12345abcefg">

所需的Xml输出(原始Xml文档)

<?xml version="1.0" encoding="utf-8"?>
<DataConfiguration xmln="abcefg12345" xmlns:xsi="12345abcefg" xsi:schemaLocation="12345abcefg12345abcefg">
  <Hosts>
    <Sites>
        <Site Name="ss">
            <Host Id="aa">
                <Address Host="www.www.com"> </Address>
            </Host>
            <Host Id="ee">
                <Address Host="www.www.com"> </Address>
            </Host>
            <Host Id="dd">
                <Address Host="www.www.com"> </Address>
            </Host> 
            <Host Id="pp">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com/"/>
            </Host>
            <Host Id="ss">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host> 
            <Host Id="561">
                <Address Host="www.www.com"> </Address>
            </Host> 
        </Site>
        <Site Name="hihi">
            <Host Id="cc">
                <Address Host="www.www.com"> </Address>
            </Host>
            <Host Id="sdD">
                <Address Host="www.www.com"> </Address>
            </Host>
            <Host Id="8uj">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>

            </Host>
            <Host Id="222">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>               
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>             
        </Site>     
    </Sites>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>         
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>

</Hosts>
<DataPools>
    <DataPool Id="sss" default="sure">
        <DataGroup Id="sss" Parent="aaa" UserCanSelectHost="sure" >
            <HostId Parent="hhhh">I'm breaking here</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="ccc" UserCanSelectHost="whynot" >
            <HostId>God I'm breaking here again, i hope you can fix me</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="sss" UserCanSelectHost="yessure" >
            <HostId>cry face</HostId>
                <webfg displaygroup="sss" provider="sss" id="ccc" principal="ccc" nioarc="ccc" nap="ccc" group="ccc">
                </webfg>

                <nhood port="1234"/>
            <ServerNames>
              <!-- insert comment -->
              <!-- insert comment -->
              <!-- insert comment -->
              <ServerName>myname</ServerName>
              <ServerName>yourname</ServerName>
            </ServerNames>
            <!-- insert comment -->
            <Implementations>
              <Implementation>
                <Name>yourname</Name>
                <Type>typeme</Type>
                <Assembly>visionme</Assembly>
                <Path>ohno</Path>
              </Implementation>
            </Implementations>-->
                <cfgman port="ccc" />               
                <webservice provider="ccc"  />
                <webservice provider="ccc"  />
                <webservice provider="ccc"  />
                    <parameters>
                        <useeventpush value="ccc"/>
                    </parameters>
                <webservice provider="ccc"  />
                        <pollingFrequency value="1000"/>
        </DataGroup>
    </DataPool>
    <DataGroup Id="ccc " UserCanSelectHost="ccc" >
        <DataGroup Id="ccc " UserCanSelectHost="ccc" >
            <HostId>idk</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="ccc " UserCanSelectHost="ccc" >
            <HostId>idk</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="default" UserCanSelectHost="true" >
            <HostId>idk</HostId>
        </DataGroup>
    </DataGroup>    
</DataPools>    
</DataConfiguration>

正如您所看到的,缩进与我的输出有点不同,我的目的是让用户编辑树节点上的信息并将其保存回原始文件。

1 个答案:

答案 0 :(得分:0)

public void exportToXml(TreeView tv, string filename)
    {
        sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
        //Write the header
        sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        //Write our root node
        sr.WriteLine("<" + treeView1.Nodes[0].Text + ">");
        foreach (TreeNode node in tv.Nodes)
        {
            int depth = 1;
            saveNode(node.Nodes, depth);
        }
        //Close the root node
        sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");
        sr.Close();
    }

    private void saveNode(TreeNodeCollection tnc, int depth)
    {
        foreach (TreeNode node in tnc)
        {
            for(int i =0; i<depth;i++)
            {
                sr.Write("\t");
            }

            if (node.Nodes.Count > 0)
            {
                sr.WriteLine("<" + node.Text + ">");
                saveNode(node.Nodes, depth + 1);
                for(int i =0; i<depth;i++)
                {
                    sr.Write("\t");
                }
                sr.WriteLine("</" + node.Text.Split()[0] + ">");
            }
            else //No child nodes, so we just write the text
                sr.WriteLine("<" + node.Text + "/>");    
        }    
    }

如果您要使用代码,则需要一个变量来计算递归的深度并使用该数量的选项卡