2013-05-21 3 views
0

привет, как я могу использовать строку, которая имеет ту же структуру, что и XML-файл для дерева. Для примера здесь является частью строки coontent ..Как я могу использовать строку, которая имеет ту же структуру, что и XML-файл для дерева?

<?xml version="1.0" encoding="UTF-8"?> 
<LM-X STAT_VERSION="3.32"> 
<LICENSE_PATH TYPE="NETWORK" HOST="[email protected]" SERVER_VERSION="4.4.4" UPTIME="53 day(s) 23 hour(s) 0 min(s) 3 sec(s)"> 
<FEATURE NAME="GlobalZoneEU" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="111720" TOTAL_LICENSES="147000" SHARE="CUSTOM ,VIRTUAL"> 
<USER NAME="SYSTEM" HOST="SERV171" IP="172.16.11.115" USED_LICENSES="2000" LOGIN_TIME="2013-04-17 12:42" CHECKOUT_TIME="2013-04-17 12:42" SHARE_CUSTOM="hweuser:172.16.11.115"/> 
> 
.... 

это только строка.

Я хочу использовать эту строку, как файл xml ... Могу ли я преобразовать эту строку в (виртуальный) xml и заполнить мое дерево с помощью узлов FEATURE или USER?

мой C# код:

private void btnShowLicstate_Click(object sender, EventArgs e) 
     { 
      string command = "\"C:\\lmxendutil.exe\" -licstatxml -host serv005 -port 6200"; 

      string output = ExecuteCommand(command); 
      string final_output = output.Substring(90, output.Length-90); 

      // here i want to parse the string in a xml format string xD and load this in the  treeview 

      txtOutput.Text = final_output; 
     } 

     static string ExecuteCommand(string command) 
     { 
      int exitCode; 
      ProcessStartInfo processInfo; 
      Process process; 

      processInfo = new ProcessStartInfo("cmd.exe", "/c " + command); 
      processInfo.CreateNoWindow = true; 
      processInfo.UseShellExecute = false; 
      processInfo.RedirectStandardError = true; 
      processInfo.RedirectStandardOutput = true; 

      process = Process.Start(processInfo); 

      string output = process.StandardOutput.ReadToEnd(); 
      exitCode = process.ExitCode; 

      process.Close(); 

      return output; 
     } 

ответ

1

Используйте XDocument класс, вы можете создать один из вашей строки с XDocument.Parse (String) или XDocument.Load (Path).

Затем используйте следующие функции для заполнения вашего Tree View (найденный в http://social.msdn.microsoft.com/forums/...):

private void BuildTree(TreeView treeView, XDocument doc) 
    { 
     TreeNode treeNode = new TreeNode(doc.Root.Name.LocalName); 
     treeView.Nodes.Add(treeNode); 
     BuildNodes(treeNode, doc.Root); 
    } 

    private void BuildNodes(TreeNode treeNode, XElement element) 
    { 
     foreach (XNode child in element.Nodes()) 
     { 
      switch (child.NodeType) 
      { 
       case XmlNodeType.Element: 
        XElement childElement = child as XElement; 
        TreeNode childTreeNode = new TreeNode(childElement.Name.LocalName); 
        treeNode.Nodes.Add(childTreeNode); 
        BuildNodes(childTreeNode, childElement); 
        break; 
       case XmlNodeType.Text: 
        XText childText = child as XText; 
        treeNode.Nodes.Add(new TreeNode(childText.Value)); 
        break; 
      } 
     } 
    } 
} 
0

Вот пример связывания документа XML в виде дерева:

// we cannot bind the TreeView directly to an XmlDocument 
// so we must create an XmlDataSource and assign the XML text 
XmlDataSource XDdataSource = new XmlDataSource(); 
XDdataSource.ID = DateTime.Now.Ticks.ToString(); // unique ID is required 
XDdataSource.Data = XDoc.OuterXml; 

// we want the full name displayed in the tree so 
// do custom databindings 
TreeNodeBinding Binding = new TreeNodeBinding(); 
Binding.TextField = "FullName"; 
Binding.ValueField = "ID"; 
TreeView1.DataBindings.Add(Binding); 

// Hook up the XmlDataSource to the TreeView 
TreeView1.DataSource = XDdataSource; 
TreeView1.DataBind(); 
+0

Я не использовать asp.net – Tarasov

Смежные вопросы