使用适当的缩进C

使用适当的缩进C

本文介绍了使用适当的缩进C#将Treeview转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Treeview导出到带有正确缩进的Xml文件时遇到问题,我浏览了论坛,但找不到任何内容.

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

这里是有关如何将Xml写入Treeview的链接递归,将具有属性的xml文件解析为treeview c#

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

这是我的代码尝试将Treeview导出为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>

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

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.

推荐答案

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

这篇关于使用适当的缩进C#将Treeview转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!