Recently, when I used C # To import relevant content from PDM to TreeView, I encountered a very strange problem. Please give me some advice. Thank you!
Private void ReadXml (string strFileName)
{
Try
{
XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load ("c: xxx. pdm ");
TreeNode rootNode = new TreeNode ("PDM original document structure ");
TrvXML. Nodes. Clear ();
This. trv_end.Nodes.Clear ();
Tn_end2 = new TreeNode ("PDM table structure ");
Trv_end.Nodes.Add (tn_end2 );
XmlNodeList rootList = xmlDoc. ChildNodes;
Foreach (XmlNode xmlnode in rootList)
{
TreeNode tn = new TreeNode (xmlnode. Name );
If (xmlnode. HasChildNodes)
{
RootNode. Nodes. Add (tn );
AnalyseXML (xmlnode, tn );
}
}
This. trvXML. Nodes. Add (rootNode );
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
Private void AnalyseXML (XmlNode node, TreeNode tnode)
{
Try
{
XmlNodeList xnlAll = node. ChildNodes;
TreeNode tn_end = new TreeNode ();
TreeNode tn = new TreeNode ();
String strChineseName = "";
Bool TagFlag = false;
Bool TagFlag2 = false;
Foreach (XmlNode snode in xnlAll)
{
// Form the function tree of the original PDM document
If (snode. NodeType = XmlNodeType. Text)
{
Tn = new TreeNode (snode. Value + "[" + snode. NodeType. ToString () + "]");
}
Else
{
Tn = new TreeNode (snode. Name + "(" + snode. NodeType. ToString () + ")");
}
// Complete the function tree of the original PDM document
// Fill in the PDM table structure function tree
If (snode. NodeType = XmlNodeType. Element & snode. ParentNode. Name. Equals ("o: Table "))
{
// Obtain the Chinese Table Name
If (snode. Name. Equals ("a: Name "))
{
StrChineseName = snode. FirstChild. Value;
}
// English name of the table
If (snode. Name. Equals ("a: Code "))
{
Tn_end = new TreeNode (strChineseName + "[" + snode. FirstChild. Value + "]", 100,100 );
This. trv_end.TopNode.Nodes.Add (tn_end );
This. trv_end.SelectedNode = tn_end;
Tn_end2 = tn_end;
TagFlag = true;
}
// Table description
If (snode. Name. Equals ("a: Comment "))
{
TagFlag2 = true;
Tn_end.Tag = snode. FirstChild. Value + "[" + tn_end.Text.Substring (tn_end.Text.IndexOf ("[") + 1, struct ("_")-tn_end.Text.IndexOf ("["));
}
If (TagFlag &&! TagFlag2)
{
Tn_end.Tag = "[" + tn_end.Text.Substring (tn_end.Text.IndexOf ("[") + 1, struct ("_", struct ("[")-tn_end.Text.IndexOf ("["));
}
}
// Complete the function tree for filling the PDM table structure
// Fill fields in each column of the table
If (snode. ParentNode. Name. Equals ("o: Column") & snode. ParentNode. ParentNode. Name. Equals ("c: Columns "))
{
If (snode. Name. Equals ("a: Name "))
{
Tn_end = new TreeNode (snode. FirstChild. Value, 1000,1000 );
Trv_end.SelectedNode.Nodes.Add (tn_end );
Trv_end.SelectedNode = tn_end;
}
// English name of the field
If (snode. Name. Equals ("a: Code "))
{
Tn_end = new TreeNode (snode. FirstChild. Value );
Trv_end.SelectedNode.Nodes.Add (tn_end );
}
// Field description
If (snode. Name. Equals ("a: Comment "))
{
Tn_end = new TreeNode (snode. FirstChild. Value );
Trv_end.SelectedNode.Nodes.Add (tn_end );
}
// Field Type
If (snode. Name. Equals ("a: DataType "))
{
Tn_end = new TreeNode (snode. FirstChild. Value );
Trv_end.SelectedNode.Nodes.Add (tn_end );
Trv_end.SelectedNode = tn_end2;
}
}
// Fill fields in each column
Tnode. Nodes. Add (tn );
If (snode. HasChildNodes)
{
AnalyseXML (snode, tn );
}
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}