none
XML file reading - stylesheet issues

    Question

  • Hi

    When i try to read the following XML file and add it to a treeview i run into problems with two methods
    a) text read
    b) xml read

    The issue is the 2nd line, the <?xml-stylesheet

    IF i remove the "<?xml-stylesheet " line, i can get the treeview being populated on,

    with it there, no, i found a code sample which used an XML reader and the use of reader.MoveToContent();
    however this results in just one node, the top node.

    >> I am after pointers how to respove this issue as i don't want to have to manually edit XML files to remove the stylesheet statement.

    Thanks in advance

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="1.xsl"?>   <== Problem line
    <TestRun xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="6b2e3387-1bf0-43b5-bf92-1df31723afca" name="Build@VD-BLDCS 2013-07-05 20:11:46_Win32_Release" runUser="CITSYD\Build" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
      <TestSettings name="BuildServer" id="f02d2bf0-a34b-4093-950a-63f96662b942">
    ......

           private void xmlview_Load(object sender, EventArgs e)
            {
    
                XmlDocument xmldoc = new XmlDocument();
    
                XmlNode xmlnode;
    
                /* This works if ?xml-stylesheet is removed
                FileStream fs = new FileStream(MainFormClass.MainForm.fileXML.Text, FileMode.Open, FileAccess.Read);
                xmldoc.Load(fs);
                 */
                // This does not work with ?xml-stylesheet in the file
    , get single node            // 
                XmlTextReader reader = new XmlTextReader(MainFormClass.MainForm.fileXML.Text);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                reader.MoveToContent();
                xmldoc.Load(reader);
                //
    
                xmlnode = xmldoc.ChildNodes[1];
                treeView1.Nodes.Clear();
                treeView1.Nodes.Add(new TreeNode(xmldoc.DocumentElement.Name));
                TreeNode tNode;
                tNode = treeView1.Nodes[0];
                AddNode(xmlnode, tNode);
    
            }
    
            private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
            {
    
                XmlNode xNode;
                TreeNode tNode;
                XmlNodeList nodeList;
    
                int i = 0;
    
                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
                {
                    inTreeNode.Text = inXmlNode.InnerText.ToString();
                }
            }



    Monday, October 21, 2013 12:09 AM

Answers

  • Hi ,

    When you Load an XML file to a XML document object it will load the nodes as below.

    Ex :- suppose you XML structure is as below

    <?xml version="1.0" encoding="UTF-8"?>
    <Root>
      <child1>
        <child2></child2>
      </child1>
    </Root>

    If you add a quick watch an see the XML document object after loading the file it will have the following structure.

    Document.ChilNodes=<Root>

    Document.ChilNodes[0].ChildNodes=<Child1>

    Document.ChilNodes[0].ChilNodes[0].ChildNodes=<Child2>

    This will load in a tree structure. You cant access <child1> node by Document.ChilNodes[1].

    See the screeshot below when I debug you code. xmldoc.ChildNodes has only one node.

    Hope this explains what you need.

    Monday, October 21, 2013 4:49 AM

All replies

  • FYI - When i changed

                xmlnode = xmldoc.ChildNodes[1];

    to

                xmlnode = xmldoc.ChildNodes[0];
    it worked! (XmlReader code)

    => So bonus points if someone can explain why ?

    Thanks

    Monday, October 21, 2013 4:21 AM
  • Hi ,

    When you Load an XML file to a XML document object it will load the nodes as below.

    Ex :- suppose you XML structure is as below

    <?xml version="1.0" encoding="UTF-8"?>
    <Root>
      <child1>
        <child2></child2>
      </child1>
    </Root>

    If you add a quick watch an see the XML document object after loading the file it will have the following structure.

    Document.ChilNodes=<Root>

    Document.ChilNodes[0].ChildNodes=<Child1>

    Document.ChilNodes[0].ChilNodes[0].ChildNodes=<Child2>

    This will load in a tree structure. You cant access <child1> node by Document.ChilNodes[1].

    See the screeshot below when I debug you code. xmldoc.ChildNodes has only one node.

    Hope this explains what you need.

    Monday, October 21, 2013 4:49 AM