Asked  7 Months ago    Answers:  5   Viewed   57 times

I'm currently working on a project where an application will take on XML files and display it into a treeview in C#. I'm using Visual Studio 10 to write this code.

I cannot limit number of times the attributes are displayed. I used a foreach loop to loop through each of the attributes it has and display it, but it's displaying the attributes once for each childnode it has under the node. How can I modify this code to only display the attributes once?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace xmlToTreeview
{
    public partial class Form1 : Form
    {
        string samplePath = Application.StartupPath + @"\sample.xml";
        public Form1()
        {
            InitializeComponent();
            DisplayTreeView(samplePath);
        }

        private void DisplayTreeView(string pathname)
        {
            try
            {
                // SECTION 1. Create a DOM Document and load the XML data into it.
                XmlDocument dom = new XmlDocument();
                dom.Load(pathname);


                // 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);

            }
            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];

                    //Check if the XmlNode has attributes
                    if (inXmlNode.Attributes.Count != 0)
                    {
                        foreach (XmlAttribute att in inXmlNode.Attributes)
                        {
                            inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
                        }
                    }
                    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();
            }
            treeView1.ExpandAll();
        }
    }
}

And here is an example of my xml

<?xml version="1.0" encoding="utf-8"?>
<DataConfiguration xmln="abcefg12345" xmlns:xsi="12345abcefg" xsi:schemaLocation="12345abcefg12345abcefg">
  <Hosts>
    <Sites>
        <Site Name="ss">
            <Host Id="aa">
                <Address Host="www.www.com"> </Address>
            </Host>
            <Host Id="ee">
                <Address Host="www.www.com"> </Address>
            </Host>
            <Host Id="dd">
                <Address Host="www.www.com"> </Address>
            </Host> 
            <Host Id="pp">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com/"/>
            </Host>
            <Host Id="ss">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host> 
            <Host Id="561">
                <Address Host="www.www.com"> </Address>
            </Host> 
        </Site>
        <Site Name="hihi">
            <Host Id="cc">
                <Address Host="www.www.com"> </Address>
            </Host>
            <Host Id="sdD">
                <Address Host="www.www.com"> </Address>
            </Host>
            <Host Id="8uj">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>

            </Host>
            <Host Id="222">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>               
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>             
        </Site>     
    </Sites>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>         
            <Host Id="hhh">
                <Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/>
            </Host>

</Hosts>
<DataPools>
    <DataPool Id="sss" default="sure">
        <DataGroup Id="sss" Parent="aaa" UserCanSelectHost="sure" >
            <HostId Parent="hhhh">I'm breaking here</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="ccc" UserCanSelectHost="whynot" >
            <HostId>God I'm breaking here again, i hope you can fix me</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="sss" UserCanSelectHost="yessure" >
            <HostId>cry face</HostId>
                <webfg displaygroup="sss" provider="sss" id="ccc" principal="ccc" nioarc="ccc" nap="ccc" group="ccc">
                </webfg>

                <nhood port="1234"/>
            <ServerNames>
              <!-- insert comment -->
              <!-- insert comment -->
              <!-- insert comment -->
              <ServerName>myname</ServerName>
              <ServerName>yourname</ServerName>
            </ServerNames>
            <!-- insert comment -->
            <Implementations>
              <Implementation>
                <Name>yourname</Name>
                <Type>typeme</Type>
                <Assembly>visionme</Assembly>
                <Path>ohno</Path>
              </Implementation>
            </Implementations>-->
                <cfgman port="ccc" />               
                <webservice provider="ccc"  />
                <webservice provider="ccc"  />
                <webservice provider="ccc"  />
                    <parameters>
                        <useeventpush value="ccc"/>
                    </parameters>
                <webservice provider="ccc"  />
                        <pollingFrequency value="1000"/>
        </DataGroup>
    </DataPool>
    <DataGroup Id="ccc " UserCanSelectHost="ccc" >
        <DataGroup Id="ccc " UserCanSelectHost="ccc" >
            <HostId>idk</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="ccc " UserCanSelectHost="ccc" >
            <HostId>idk</HostId>
            <DataSources>
                <empty/>
            </DataSources>
        </DataGroup>
        <DataGroup Id="default" UserCanSelectHost="true" >
            <HostId>idk</HostId>
        </DataGroup>
    </DataGroup>    
</DataPools>    
</DataConfiguration>

 Answers

85

You need to move the loop through attributes out of the loop through child nodes:

    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)
        {
            //Check if the XmlNode has attributes
            foreach (XmlAttribute att in inXmlNode.Attributes)
            {
                inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
            }

            var nodeList = inXmlNode.ChildNodes;
            for (int i = 0; i < nodeList.Count; i++)
            {
                var xNode = inXmlNode.ChildNodes[i];
                var tNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(xNode.Name))];
                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();
        }
        treeView1.ExpandAll();
    }

Update

If you want to filter out namespace attributes, you can add extension methods:

public static class XmlNodeExtensions
{
    public static bool IsDefaultNamespaceDeclaration(this XmlAttribute attr)
    {
        if (attr == null)
            return false;
        if (attr.NamespaceURI != "http://www.w3.org/2000/xmlns/")
            return false;
        return attr.Name == "xmlns";
    }

    public static bool IsNamespaceDeclaration(this XmlAttribute attr)
    {
        if (attr == null)
            return false;
        if (attr.NamespaceURI != "http://www.w3.org/2000/xmlns/")
            return false;
        return attr.Name == "xmlns" || attr.Name.StartsWith("xmlns:");
    }
}

Then use it to skip unwanted XmlAttribute instances. You can also explicitly set the text of all nodes of type XmlElement to be name + attribute data, not just those elements with children, using OuterXml only for text nodes:

    private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
    {
        if (inXmlNode is XmlElement)
        {
            // An element.  Display element name + attribute names & values.
            foreach (var att in inXmlNode.Attributes.Cast<XmlAttribute>().Where(a => !a.IsNamespaceDeclaration()))
            {
                inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
            }
            // Add children
            foreach (XmlNode xNode in inXmlNode.ChildNodes)
            {
                var tNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(xNode.Name))];
                AddNode(xNode, tNode);
            }
        }
        else
        {
            // Not an element.  Character data, comment, etc.  Display all text.
            inTreeNode.Text = (inXmlNode.OuterXml).Trim();
        }
        treeView1.ExpandAll();
    }

If you really want to filter out just the default namespace definitions but leave others, you could do:

            // An element.  Display element name + attribute names & values.
            foreach (var att in inXmlNode.Attributes.Cast<XmlAttribute>().Where(a => !a.IsDefaultNamespaceDeclaration()))
            {
                inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
            }

Incidentally, I don't recommend doing this since the following actually mean the same thing, namely an element with local name DataConfiguration in the namespace http://somenamespace:

<ss:DataConfiguration xmlns:ss="http://somenamespace"/>
<DataConfiguration xmlns="http://somenamespace"/>

Your tree will display the namespace for the first element but not the second.

Update 2

To include the XmlDeclaration in the tree, change the top level loop to be:

        treeView1.Nodes.Clear();
        foreach (XmlNode xNode in dom.ChildNodes)
        {
            var tNode = treeView1.Nodes[treeView1.Nodes.Add(new TreeNode(xNode.Name))];
            AddNode(xNode, tNode);
        }

Update 3

Put the loop to include the XmlDeclaration loop in DisplayTreeView:

    private void DisplayTreeView(string pathname)
    {
        try
        {
            // SECTION 1. Create a DOM Document and load the XML data into it.
            XmlDocument dom = new XmlDocument();
            dom.Load(pathname);

            // SECTION 2. Initialize the TreeView control.
            treeView1.Nodes.Clear();

            // SECTION 3. Populate the TreeView with the XML nodes.
            foreach (XmlNode xNode in dom.ChildNodes)
            {
                var tNode = treeView1.Nodes[treeView1.Nodes.Add(new TreeNode(xNode.Name))];
                AddNode(xNode, tNode);
            }
        }
        catch (XmlException xmlEx)
        {
            MessageBox.Show(xmlEx.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
Tuesday, June 1, 2021
 
VostanAzatyan
answered 7 Months ago
52

There are a number of sample controls to be found around the web:

  • TreeViewAdv for .Net
  • TreeView with Columns
  • ContainerListView and TreeListView

But the all-time favorite is probably the ObjectListView, which provides an expandable, multi-column ListView, along with many other incredibly handy features:

   ObjectListView sample

Monday, June 28, 2021
 
buymypies
answered 6 Months ago
33

First you need to create an object that does the parsing. It will instatiate the NSXMLParser instance, set itself as the delegate for the parser and then call the parse message. It can also be responsible for storing your four result arrays:

NSXMLParser * parser = [[NSXMLParser alloc] initWithData:_data];
[parser setDelegate:self];
BOOL result = [parser parse];

The message you are most interested in implementing in your delegate objects is didStartElement. This guy gets called for each element in your XML file. In this callback you can add your name, price & where attributes to their respective arrays.

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict
{
    // just do this for item elements
    if(![elementName isEqual:@"item"])
        return;

    // then you just need to grab each of your attributes
    NSString * name = [attributeDict objectForKey:@"name"];

    // ... get the other attributes

    // when we have our various attributes, you can add them to your arrays
    [m_NameArray addObject:name];

    // ... same for the other arrays
}
Wednesday, July 28, 2021
 
muffe
answered 4 Months ago
13
import java.io.IOException;
import java.net.URL;
import org.apache.xerces.parsers.DOMParser;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class XMLParser {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        parseXml2("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=nucleotide&id=224589801");
    }

    public static void parseXml2(String URL) {
        DOMParser parser = new DOMParser();

        try {
            parser.parse(new InputSource(new URL(URL).openStream()));
            Document doc = parser.getDocument();

            NodeList nodeList = doc.getElementsByTagName("Item");
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.print("Item "+(i+1));
                Node n = nodeList.item(i);
                NamedNodeMap m = n.getAttributes();
                System.out.print(" Name: "+m.getNamedItem("Name").getTextContent());
                System.out.print(" Type: "+m.getNamedItem("Type").getTextContent());
                Node actualNode = n.getFirstChild();
                if (actualNode != null) {
                    System.out.println(" "+actualNode.getNodeValue());
                } else {
                    System.out.println(" ");                    
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Completed the sample code and added a few lines to get the attributes.

This should get you started, although I feel that you need to get yourself up to date with the basic notions of DOM. This site (and many others) can help you with that. Most importantly is understanding the different kinds of nodes there are.

Saturday, August 28, 2021
 
viper
answered 3 Months ago
89

Am I doing something wrong?

I don't think so. I share your opinion that XmlSerializer ought to deserialize its own output without any warnings. Also, xsi:type is a standard attribute defined in the XML Schema specification, and obviously it is supported by XmlSerializer, as demonstrated by your example and documented in MSDN Library.

Therefore, this behavior simply looks like an oversight. I can imagine a group of Microsoft developers working on different aspects of XmlSerializer during the development of the .NET Framework, and not ever testing xsi:type and events at the same time.

That means that the serializer evaluates the type information properly during an early stage in the deserialization. But during a later data-extraction stage the attribute is apparently mistaken for a true data part of the object, which is of course unknown.

Your observation is correct.

The XmlSerializer class generates a dynamic assembly to serialize and deserialize objects. In your example, the generated method that deserializes instances of DerivedT looks something like this:

private DerivedT Read2_DerivedT(bool isNullable, bool checkType)
{
    // [Code that uses isNullable and checkType omitted...]

    DerivedT derivedT = new DerivedT();
    while (this.Reader.MoveToNextAttribute())
    {
        if (!this.IsXmlnsAttribute(this.Reader.Name))
            this.UnknownNode(derivedT);
    }

    this.Reader.MoveToElement();
    // [Code that reads child elements and populates derivedT.anInt omitted...]
    return derivedT;
}

The deserializer calls this method after it reads the xsi:type attribute and decides to create an instance of DerivedT. As you can see, the while loop raises the UnknownNode event for all attributes except xmlns attributes. That's why you get the UnknownNode (and UnknownAttribute) event for xsi:type.

The while loop is generated by the internal XmlSerializationReaderILGen.WriteAttributes method. The code is rather complicated, but I see no code path that would cause xsi:type attributes to be skipped (other than the second workaround I describe below).

Is there a workaround?

I would just ignore UnknownNode and UnknownAttribute events for xsi:type:

private static void serializer_UnknownNode(object sender, XmlNodeEventArgs e)
{
    if (e.NodeType == XmlNodeType.Attribute &&
        e.NamespaceURI == XmlSchema.InstanceNamespace && e.LocalName == "type")
    {
        // Ignore xsi:type attributes.
    }
    else
    {
        // [Log it...]
    }
}

// [And similarly for UnknownAttribute using e.Attr instead of e...]

Another (hackier, IMO) workaround is to map xsi:type to a dummy property in the BaseT class:

[XmlInclude(typeof(DerivedT))]
public class BaseT
{
    [XmlAttribute("type", Namespace = XmlSchema.InstanceNamespace)]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)] // Hide this useless property
    public string XmlSchemaType
    {
        get { return null; } // Must return null for XmlSerializer.Serialize to work
        set { }
    }
}
Wednesday, October 20, 2021
 
foo
answered 1 Month ago
foo
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share