Treeview to XML with proper indentation c#

ぐ巨炮叔叔 提交于 2019-12-12 01:55:15

问题


I'm having trouble exporting Treeview to a Xml file with proper indentation, I looked through the forum but could not find anything.

Here is a link on how to write Xml to Treeview Recursion, parsing xml file with attributes into treeview c#

Here is my code attempting to export treeview to 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 Output

<?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">

Desired Xml Output (Original Xml Document)

<?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>

As you can see, the indentation is kinda off from my output, my purpose is to let user edit information on the tree nodes and save it back to the original file.


回答1:


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 + "/>");    
        }    
    }

if you are going to use your code you need a variable to count how deep you are in recursion and use that number of tabs



来源:https://stackoverflow.com/questions/29087531/treeview-to-xml-with-proper-indentation-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!