1

I new ti XML and I'm struggling with some basics.

I have a XMLTextReader that reads through a document:

<node guid="e7071d4d-21b0-48c0-b803-daf202998633" tag="3DMesh" imageindex="1" text="body__6557_0"/>

<node guid="3bc0240f-cdf4-4e09-94de-a52d7bc0c4be" tag="3DMesh" imageindex="1" text="body__6558_0"/>

<node guid="59dd7ce9-176c-4a45-a45d-b632b675b6c9" tag="3DMesh" imageindex="1" text="body__6581_0"/>

<node guid="66166789-3500-46ef-b43b-34919481ad37" tag="3DMesh" imageindex="1" text="body__6582_0"/>

<node guid="95882524-a9a6-4a50-a3bc-fe7c31ef2e47" tag="3DMesh" imageindex="1" text="Fixed_Support_OD_610_1__1_0"/>


<node tag="GroupNode" imageindex="3" text="Group">

<node guid="295e5a94-3732-4b3d-81f4-b04636eb71c3" tag="3DMesh" imageindex="1" text="a_11_1__9_0"/>

<node guid="f56472cc-dd46-44c8-997f-113cb959661d" tag="3DMesh" imageindex="1" text="a_12_1__9_0"/>

</node>

<node guid="2ebcd33a-f8e7-46ea-b637-2b7e3120a7da" tag="3DMesh" imageindex="1" text="Rod_Hanger_610_1_0"/>

<node guid="9221b254-fe4c-4c10-b6d2-da21a46eeb56" tag="3DMesh" imageindex="1" text="Rod_Hanger_610_2_0"/>

<node guid="38b0b0ef-0a62-4392-a607-ce1e708503a3" tag="3DMesh" imageindex="1" text="Single_variable_spring_hanger1_610_1_0"/>

<node guid="c41ab0c6-2f91-4595-b015-204675d8eaa4" tag="3DMesh" imageindex="1" text="Single_variable_spring_hanger1_610_2_0"/>

<node guid="1a01282c-c82f-4c63-afc4-35636972b499" tag="3DMesh" imageindex="1" text="Single_variable_spring_hanger1_610_3_0"/>

I get to the "GroupNode", then I need to save the GUID of the child elements. To do that... I'm thinking of looping through them and getting the info i need.

I cant seem to get the amount of of children so I can do a forloop through them.

Please help.

Here I deserialize the file and create a tree structure(3D) from the XML.

  public void Deserialize(UserControl_Designer_Tree TreeView, string FileName)
    {
        XmlTextReader reader = null;
        try
        {                
            TreeView.Multiselect_Tree_View.BeginUpdate(); //Disabling re-drawing of treeview till all nodes are added.

            reader = new XmlTextReader(FileName);

            TreeNode parentNode = null;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == XmlNodeTag)
                    {
                        TreeNode newNode = new TreeNode();
                        bool isEmptyElement = reader.IsEmptyElement;

                        newNode = Node_Create(reader, TreeView);
.........

The following snippet comes from the Node_Create() methos where I return the types of nodes.

if (nodeTag == "GroupNode")
        {
            XmlDocument doc = new XmlDocument();
            XmlNode myNode = doc.ReadNode(Reader);  //Get current group node

            List<string> meshGUIDs = new List<string>();
            int y = myNode.ChildNodes.Count; //The the amount of nodes in that group

            if (y > 0)
            {
                for (int a = 0; a < y; a++)
                {

                    Reader.Read();
                    nodeGuid = Reader.GetAttribute(XmlNodeGuidAttribute);
                    meshGUIDs.Add(nodeGuid);
                }

                List<Basic_Mesh> meshes = new List<Basic_Mesh>(meshGUIDs.Count);

                foreach (string meshGuiD in meshGUIDs)
                {
                    Basic_Mesh mesh = Mesh_Guid_Search(Tree.Designer_3D._3DModels_List, meshGuiD);
                    meshes.Add(mesh);
                }

                Group_Data groupData = Tree.Designer_3D._3DGrouping.Create("Group", meshes); //Create a Basic_Group object in the clarity control.
                return new Group_Node(nodeText, new List<Mesh_Node>(), groupData);
            }
        }
4
  • At what state is your Reader when you are getting into this section of the code? What happens before this section with the Reader? Oct 13, 2014 at 7:06
  • Is this the complete XML or just an extract of it? Are there going to be multiple "GroupNode" nodes in the document?
    – ilikebeets
    Oct 13, 2014 at 7:23
  • This is not the complete XML. There could be multiple "GroupNodes" with any number of "children". Oct 13, 2014 at 7:48
  • Updated post with code before the previous section. Oct 13, 2014 at 7:49

1 Answer 1

0

This worked for me...

while (Reader.Read())
{
if (Reader.NodeType == XmlNodeType.EndElement)
break;

nodeGuid = Reader.GetAttribute(XmlNodeGuidAttribute);
meshGUIDs.Add(nodeGuid);
}

Don't know if this is the best way of doing it, but it works in my case.

Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.