我正在尝试使用来自Web服务调用的返回数据填充Treeview。我使用Restsharp进行调用并返回数据。但是,我真的很难弄清楚如何将它传递到适当的树视图中。我一直在尝试使用this CodeProject guide - 但我无法让它工作,我创造了一堆我的代码试图适应它。任何帮助或方向将不胜感激。
XML返回为:
<ListItems xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Items>
<ListItem>
<ID>n0z5tVQXsbTgB8PUp7tE5$p5shhQOV3s5Y</ID>
<URI>https://URL/n0z5tVQXsbTgB8PUp7tE5$p5shhQOV3s5Y</URI>
<ListID>nnK2NXzfmoCyTPE0yck44Nswy1Ho964Lo</ListID><Name>BATMAN</Name><ParentID xsi:nil="true" />
<Level1Code>BATMAN</Level1Code><Level2Code xsi:nil="true" /><Level3Code xsi:nil="true" />
<Level4Code xsi:nil="true" /><Level5Code xsi:nil="true" /><Level6Code xsi:nil="true" />
<Level7Code xsi:nil="true" /><Level8Code xsi:nil="true" /><Level9Code xsi:nil="true" />
<Level10Code xsi:nil="true" />
</ListItem>
<ListItem>
<ID>n0z5tVQXsbTnByah$pfDj0gDjm$sMYSfFYB</ID>
<URI>https://URL/n0z5tVQXsbTnByah$pfDj0gDjm$sMYSfFYB</URI>
<ListID>nnK2NXzfmoCyTPE0yck44Nswy1Ho964Lo</ListID><Name>Rig Move Project Management</Name>
<ParentID>n0z5tVQXsbTgB8PUp7tE5$p5shhQOV3s5Y</ParentID><Level1Code>BATMAN</Level1Code>
<Level2Code>RMPM</Level2Code><Level3Code xsi:nil="true" /><Level4Code xsi:nil="true" />
<Level5Code xsi:nil="true" /><Level6Code xsi:nil="true" /><Level7Code xsi:nil="true" />
<Level8Code xsi:nil="true" /><Level9Code xsi:nil="true" /><Level10Code xsi:nil="true" />
</ListItem>
<ListItem>
<ID>n0z5tVQXsbT76YjU9r5rbMbo526tKB7kT</ID>
<URI>https://URL/n0z5tVQXsbT76YjU9r5rbMbo526tKB7kT</URI>
<ListID>nnK2NXzfmoCyTPE0yck44Nswy1Ho964Lo</ListID><Name>14-RMPM-AUG</Name>
<ParentID>n0z5tVQXsbTnByah$pfDj0gDjm$sMYSfFYB</ParentID><Level1Code>BATMAN</Level1Code>
<Level2Code>RMPM</Level2Code><Level3Code>14-RMPM-AUG</Level3Code><Level4Code xsi:nil="true" />
<Level5Code xsi:nil="true" /><Level6Code xsi:nil="true" /><Level7Code xsi:nil="true" />
<Level8Code xsi:nil="true" /><Level9Code xsi:nil="true" /><Level10Code xsi:nil="true" />
</ListItem>
<ListItem>
<ID>n0z5tVQXsbT98mCa24ijg8oKVUXUu9voE</ID>
<URI>https://URL/n0z5tVQXsbT98mCa24ijg8oKVUXUu9voE</URI>
<ListID>nnK2NXzfmoCyTPE0yck44Nswy1Ho964Lo</ListID><Name>14-RMPM-SEP</Name>
<ParentID>n0z5tVQXsbTnByah$pfDj0gDjm$sMYSfFYB</ParentID><Level1Code>BATMAN</Level1Code>
<Level2Code>RMPM</Level2Code><Level3Code>14-RMPM-SEP</Level3Code><Level4Code xsi:nil="true" />
<Level5Code xsi:nil="true" /><Level6Code xsi:nil="true" /><Level7Code xsi:nil="true" />
<Level8Code xsi:nil="true" /><Level9Code xsi:nil="true" /><Level10Code xsi:nil="true" />
</ListItem>
<ListItem>
<ID>n0z5tVQXsbTzkVY2DmlIKqTJQ$ssiU$sZhq</ID>
<URI>https://URL/n0z5tVQXsbTzkVY2DmlIKqTJQ$ssiU$sZhq</URI>
<ListID>nnK2NXzfmoCyTPE0yck44Nswy1Ho964Lo</ListID><Name>14-RMPM-DEC</Name>
<ParentID>n0z5tVQXsbTnByah$pfDj0gDjm$sMYSfFYB</ParentID><Level1Code>BATMAN</Level1Code>
<Level2Code>RMPM</Level2Code><Level3Code>14-RMPM-DEC</Level3Code><Level4Code xsi:nil="true" />
<Level5Code xsi:nil="true" /><Level6Code xsi:nil="true" /><Level7Code xsi:nil="true" />
<Level8Code xsi:nil="true" /><Level9Code xsi:nil="true" /><Level10Code xsi:nil="true" />
</ListItem>
<ListItem>
<ID>n0z5tVQXsbT3akVrmqiTRzuqyw4bvZhNj</ID>
<URI>https://URL/n0z5tVQXsbT3akVrmqiTRzuqyw4bvZhNj</URI>
<ListID>nnK2NXzfmoCyTPE0yck44Nswy1Ho964Lo</ListID><Name>BOONE</Name><ParentID xsi:nil="true" />
<Level1Code>BOONE</Level1Code><Level2Code xsi:nil="true" /><Level3Code xsi:nil="true" />
<Level4Code xsi:nil="true" /><Level5Code xsi:nil="true" /><Level6Code xsi:nil="true" />
<Level7Code xsi:nil="true" /><Level8Code xsi:nil="true" /><Level9Code xsi:nil="true" />
<Level10Code xsi:nil="true" />
</ListItem>
<ListItem>
<ID>n0z5tVQXsbTJzA$pdmVRdjjTNL8ZgV2CtV</ID>
<URI>https://URL/n0z5tVQXsbTJzA$pdmVRdjjTNL8ZgV2CtV</URI>
<ListID>nnK2NXzfmoCyTPE0yck44Nswy1Ho964Lo</ListID><Name>TEST</Name>
<ParentID>n0z5tVQXsbT3akVrmqiTRzuqyw4bvZhNj</ParentID><Level1Code>BOONE</Level1Code><Level2Code>TEST</Level2Code>
<Level3Code xsi:nil="true" /><Level4Code xsi:nil="true" /><Level5Code xsi:nil="true" />
<Level6Code xsi:nil="true" /><Level7Code xsi:nil="true" /><Level8Code xsi:nil="true" />
<Level9Code xsi:nil="true" /><Level10Code xsi:nil="true" />
</ListItem>
<ListItem>
<ID>n0z5tVQXsbTJzA$pdmVRdjjTNL8ZgV2CtV</ID>
<URI>https://URL/n0z5tVQXsbTJzA$pdmVRdjjTNL8ZgV2CtV</URI>
<ListID>nnK2NXzfmoCyTPE0yck44Nswy1Ho964Lo</ListID><Name>TEST1</Name>
<ParentID>n0z5tVQXsbT3akVrmqiTRzuqyw4bvZhNj</ParentID><Level1Code>BOONE</Level1Code><Level2Code>TEST1</Level2Code>
<Level3Code xsi:nil="true" /><Level4Code xsi:nil="true" /><Level5Code xsi:nil="true" />
<Level6Code xsi:nil="true" /><Level7Code xsi:nil="true" /><Level8Code xsi:nil="true" />
<Level9Code xsi:nil="true" /><Level10Code xsi:nil="true" />
</ListItem>
</Items>
<NextPage xsi:nil="true" />
</ListItems>
所以它应该解决:
-BATMAN
--- RMPM
------ 14 RMPM-AUG
------ 14 RMPM-SEP
------ 14 RMPM-DEC
-BOONE
--- TEST
--- TEST1
答案 0 :(得分:1)
我所做的就是使用你的xml并使用VS 2013的Paste Special -> Paste XML As Class
功能从XML创建类。然后我使用XML序列化程序将XML反序列化为我可以使用的对象。然后,我为每个项目创建了树节点,并构建了适当的结构,并将根添加到树视图中。
它做了一些假设:
string xml = ""; // Your big XML string
StringReader sr = new StringReader(xml);
XmlSerializer xs = new XmlSerializer(typeof(ListItems));
ListItems listItems = (ListItems)xs.Deserialize(sr);
Dictionary<string, TreeNode> nodes = new Dictionary<string, TreeNode>();
foreach (var item in listItems.Items)
{
TreeNode node;
nodes.Add(item.ID, node = new TreeNode(item.Name));
if (item.ParentId != null)
nodes[item.ParentId].Nodes.Add(node);
else
treeView1.Nodes.Add(node);
}
/* Edited this out, can do in only one loop
foreach (var item in listItems.Items)
{
var children = from i in listItems.Items where i.ParentID == item.ID select i;
foreach (var child in children)
{
nodes[item.ID].Nodes.Add(nodes[child.ID]);
}
if (item.ParentID==null)
treeView1.Nodes.Add(nodes[item.ID]);
}
*/