質問する前に、はい、XMLに関する私の質問に関する回答について調査しましたが、有用なものをいくつか見つけた、または私のケースに非常に近いものを見つけたにもかかわらず、そのスポットオンの回答をまだ見つけていません。
ところで、私はMicrosoft Visual Studio 2005(C#)を使用しています。[Windowsアプリケーションフォーム]
私の問題:XML(Filename.vsysvar)を解析し、GUIでリストを表示すること。特定の問題:私は.vsysvarファイル拡張子にあまり精通していませんが、これらはXMLで機能しますか?個人的な問題:XMLは私にとって異質であり、私はまだそれを学ぼうとしています。
そこで、.vsysvarファイルを.xmlファイルとして保存しようとしましたが、XMLTreeViewリストの次のコードが見つかりました。
private void Form1_Load(object sender, EventArgs e)
{
// Initialize the controls and the form.
label1.Text = "File Path";
label1.SetBounds(8, 8, 50, 20);
textBox1.Text = Application.StartupPath + "\\Continental.xml";
textBox1.SetBounds(64, 8, 256, 20);
button1.Text = "Populate the TreeView with XML";
button1.SetBounds(8, 40, 200, 20);
this.Text = "TreeView control from XML";
/*this.Width = 336;
this.Height = 368;
treeView1.SetBounds(8, 72, 312, 264);*/
}
private void button1_Click(object sender, EventArgs e)
{
try
{
// SECTION 1. Create a DOM Document and load the XML data into it.
XmlDocument dom = new XmlDocument();
dom.Load(textBox1.Text);
// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];
// SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(dom.DocumentElement, tNode);
treeView1.ExpandAll();
}
catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so forth.
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}
もちろん、最初にsample.xmlで試してみましたが、うまくいきました。.vsysvarから変換された独自の.xmlを試してみると、そこから問題が発生します。これを投稿できるかどうかはわかりませんが、File.vsysvarをご覧ください。非常に長く、文字数が限られているため、ファイルのプレビューは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<systemvariables version="4">
<namespace name="" comment="">
<namespace name="_01_Test_Preparation" comment="">
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_02_Shipping_Status_Check" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_02_Shipping_Status_Check_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_01_Get_Dem_ID" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_01_Get_Dem_ID_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_04_ECU_Version_Check_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_03_Test_Run_Init" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_04_ECU_Version_Check" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_05_DEM_Reader" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_03_Test_Run_Init_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_05_DEM_Reader_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
</namespace>
<namespace name="_02_Communication" comment="">
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_04_VCAN_StartLoad" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_08_XCP_Restbus_RAM_Monitor" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_01_VCAN_Output_Cyclic" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_02_VCAN_Input" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_05_VCAN_Event_Frame" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_07_VCAN_Failsafe" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_03_VCAN_Startup" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_06_VCAN_Manipulate_Input" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_09_Communication_Min_Max_Voltage" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_10_Power_On_Mask" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_11_VCAN_IgnitionOff" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_12_J1699" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_13_Input_Data_For_Algo" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_01_VCAN_Output_Cyclic_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_02_VCAN_Input_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_07_VCAN_Failsafe_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_12_J1699_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_03_VCAN_Startup_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_04_VCAN_StartLoad_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_08_XCP_Restbus_RAM_Monitor_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_05_VCAN_Event_Frame_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_06_VCAN_Manipulate_Input_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_09_Communication_Min_Max_Voltage_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_10_Power_On_Mask_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_11_VCAN_IgnitionOff_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_13_Input_Data_For_Algo_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_14_VCAN_Bus_Off_Dection_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_14_VCAN_Bus_Off_Dection" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
</namespace>
...
</systemvariables>
上記のコードで記述された内容は、そのままWinFormにも表示されます。TreeView形式のリストに従いますが、リストに書かれているのはコードであり、私が見たかったものではありません。名前です。
必要な構造は次のようになります。
Category A
Property_A_1
Property_A_2
Property_A_3
Category B
Property_B_1
Property_B_2
Property_B_3
編集:dbcは大きな助けになりました、私はTreeViewリストをほとんどきれいにしました。
私はこれを手に入れています:
systemvariables
namespace
Category A
Property_A_1
Property_A_2
Property_A_3
Category B
Property_B_1
Property_B_2
Property_B_3
私はそれを間違っているのですか、それとも何かが足りないのですか?ありがとう。
既存のコードは、「リーフ」XMLノードのXML全体を表示しますが、非リーフノードの要素名のみを表示します。それを望まない場合は、必要なAddNode
コンテンツを表示するように変更する必要があります。
static string GetAttributeText(XmlNode inXmlNode, string name)
{
XmlAttribute attr = (inXmlNode.Attributes == null ? null : inXmlNode.Attributes[name]);
return attr == null ? null : attr.Value;
}
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
if (inXmlNode.HasChildNodes)
{
XmlNodeList nodeList = inXmlNode.ChildNodes;
for (int i = 0; i <= nodeList.Count - 1; i++)
{
XmlNode xNode = inXmlNode.ChildNodes[i];
string text = GetAttributeText(xNode, "name");
if (string.IsNullOrEmpty(text))
text = xNode.Name;
inTreeNode.Nodes.Add(new TreeNode(text));
TreeNode tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
// If the node has an attribute "name", use that. Otherwise display the entire text of the node.
string text = GetAttributeText(inXmlNode, "name");
if (string.IsNullOrEmpty(text))
text = (inXmlNode.OuterXml).Trim();
if (inTreeNode.Text != text)
inTreeNode.Text = text;
inTreeNode.Nodes.Clear();
}
}
そして結果は次のようになります
(XMLの最初の「名前空間」ノードの名前は空です。そのため、全文が引き続き表示されます。)
更新
達成したいUIを示したので、次のことを行う必要があります。
したがって:
private void LoadTreeFromXmlDocument(XmlDocument dom)
{
try
{
// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
// SECTION 3. Populate the TreeView with the DOM nodes.
foreach (XmlNode node in dom.DocumentElement.ChildNodes)
{
if (node.Name == "namespace" && node.ChildNodes.Count == 0 && string.IsNullOrEmpty(GetAttributeText(node, "name")))
continue;
AddNode(treeView1.Nodes, node);
}
treeView1.ExpandAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
static string GetAttributeText(XmlNode inXmlNode, string name)
{
XmlAttribute attr = (inXmlNode.Attributes == null ? null : inXmlNode.Attributes[name]);
return attr == null ? null : attr.Value;
}
private void AddNode(TreeNodeCollection nodes, XmlNode inXmlNode)
{
if (inXmlNode.HasChildNodes)
{
string text = GetAttributeText(inXmlNode, "name");
if (string.IsNullOrEmpty(text))
text = inXmlNode.Name;
TreeNode newNode = nodes.Add(text);
XmlNodeList nodeList = inXmlNode.ChildNodes;
for (int i = 0; i <= nodeList.Count - 1; i++)
{
XmlNode xNode = inXmlNode.ChildNodes[i];
AddNode(newNode.Nodes, xNode);
}
}
else
{
// If the node has an attribute "name", use that. Otherwise display the entire text of the node.
string text = GetAttributeText(inXmlNode, "name");
if (string.IsNullOrEmpty(text))
text = (inXmlNode.OuterXml).Trim();
TreeNode newNode = nodes.Add(text);
}
}
これは
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加