0

これに似たxmlファイルがあります

<items>
    <cla1>
        <type1>
            <unit>this is a unit</unit>
            <title>this is a title</title>
            <link></link>
        </type1>

        <type2>
            <unit>this is a unit</unit>
            <title>this is a title</title>
            <link></link>
        </type2>
    </cla1>

        <cla2>
            <type1>
                <title>this is a title</title>
                <link></link>
            </type1>
        </cla2>

</items>

この情報を使用して、ツリービュー コントロールを次のように設定します。

(画像を追加できないので、ここにリンクがあります) http://i.imgur.com/cVRDhDR.png

name of cla
 |____type1 value
 |  |____unit value
 |      |_____title value
 |____type2 value
 |  |____unit value
 |      |_____title value
 |
name of cla
 |____type1 value
 |  |____title value
 |      

私のxml構造がこれを難しくしている可能性があることは理解していますが(これは初めてです)、物事が簡単になるのであれば、これを変更してもかまいません。探しているものを達成する方法についての提案をいただければ幸いです。これは私がすでに持っているもののサンプルです。私にはこれは過剰で非効率的であるように思えます。これを行うにはもっと簡単な方法があると感じています。

//this code is in a loop going over certain nodes and
//keeps going like this until it reaches the end

if (!treeView1.Nodes.ContainsKey(cla))
{ treeView1.Nodes.Add(cla, cla); }

if (!treeView1.Nodes[cla].Nodes.ContainsKey(type))
{
treeView1.Nodes[cla].Nodes.Add(type, type);
}

ありがとう

4

1 に答える 1

0

このサンプルのように、TreeView を XMLDatasource にバインドしてみてください。

これは、タイトル ノードで少し変更した xml です。

<?xml version="1.0" encoding="utf-8" ?>
<items>
    <cla1>
        <type1>
            <unit>this is a unit</unit>
            <title text="this is a titl1" link="/Default.aspx"></title>
            <link></link>
        </type1>
        <type2>
            <unit>this is a unit</unit>
            <title text="this is a titl2" link="/Default.aspx"></title>
            <link></link>
        </type2>
    </cla1>
    <cla2>
        <type1>
            <title text="this is a titl3" link="/Default.aspx"></title>
            <link></link>
        </type1>
    </cla2>
</items>

次に、aspx部分にバインディングコードを作成する必要があります

<asp:XmlDataSource runat="server" id="xmlDataSource"  DataFile="App_Data/XMLFile.xml" />

    <asp:TreeView ID="TreeView1" runat="server"
        DataSourceID="xmlDataSource" >
        <DataBindings>
            <asp:TreeNodeBinding DataMember="title" TextField="text" NavigateUrlField="link"  />
        </DataBindings>
    </asp:TreeView>

textプロパティとlinkプロパティを持つtitle要素など、バインドするプロパティを持つ要素のみをバインドするセクション。そして、ツリー ビューに表示するテキスト プロパティの値、ナビゲート URL のように機能するリンク プロパティを設定して、コードがそのようになるようにします。プロパティをバインドしたくない他の要素については、何もする必要はありません。ツリー ビューは xml 部門として表示されます。

于 2013-11-09T03:15:06.207 に答える