Community discussion forum

How to extract specific information from XML [C# 2003]

  • 1 year ago

    I have an application that reads data from an XML file, typically I simply use the "DataTable.ReadXML(sXML)" and then extract the information that I need, but not I need to do it based on a condition and I can't seem to figure out how I can implement it.

    For example - I have an XML (C:\file.xml) such as the following:
    [Code]
    <RootNode>
        <Stores>
          <Division Name="A">
             <Info>
                <Segment Folder="FolderA1"/>
                <Segment Folder="FolderA2"/>
                <Segment Folder="FolderA3"/>
             </Info>
          </Division>
          <Division Name="B">
             <Info>
                <Segment Folder="FolderB1"/>
                <Segment Folder="FolderB2"/>
                <Segment Folder="FolderB3"/>
             </Info>
          </Division>
       <Stores>
    </RootNode>
    [/Code]

    As you can see, my XML has two divisions (name "A" and "B"), I need to get the Folder information depending on which division I am dealing with. Specifically something like this (pseudo-code):

    [Code]
    if (Division = varX)   // where varX is either "A" or "B"
       for each (string sFolder in <Segment Folder="..."> in <Info>) // Listing all the Segment Folders from either "A" or "B" depending on varX
          ... do something with sFolder ...
    [/Code]

    Now, I have the following code that simply dumps the XML into a DataTable, but I have no clue how, once with the datatable, to extract only the Folder's from <Segment Folder=".." /> of the appropriate <Info></Info> from the appropriate <Division Name=".."></Division> where the Division Name matches my search criteria...

    [Code]
    DataSet dsxmlfile = new DataSet();
    dsxmlfile.ReadXml(sXML);
    DataView dsview = dsxmlfile.Tables["Stores"].DefaultView;

    ...? stuck here ...?
    [/Code]

    Note - I don't need to use DataTable.ReadXML to perform my work, it is simply the easiest way I know how to input an XML file, however I would assume this may no longer suit my needs.

    Any help would be greatly appreciated.
    Thanks,

  • 1 year ago
    http://www.azroc.co.uk/videos/index.php This might help--->>> using System; using System.Windows.Forms; using System.Xml; public class XmlTreeDisplay : System.Windows.Forms.Form{ private System.Windows.Forms.Button cmdLoad; private System.Windows.Forms.Label lblFile; private System.Windows.Forms.TextBox txtXmlFile; private System.Windows.Forms.TreeView treeXml; // (Designer code omitted.) private void cmdLoad_Click(object sender, System.EventArgs e) { // Clear the tree. treeXml.Nodes.Clear(); // Load the XML Document XmlDocument doc = new XmlDocument(); try { doc.Load(txtXmlFile.Text); }catch (Exception err) { MessageBox.Show(err.Message); return; } // Populate the TreeView. ConvertXmlNodeToTreeNode(doc, treeXml.Nodes); // Expand all nodes. treeXml.Nodes[0].ExpandAll(); } private void ConvertXmlNodeToTreeNode(XmlNode xmlNode, TreeNodeCollection treeNodes) { // Add a TreeNode node that represents this XmlNode. TreeNode newTreeNode = treeNodes.Add(xmlNode.Name); // Customize the TreeNode text based on the XmlNode // type and content. switch (xmlNode.NodeType) { case XmlNodeType.ProcessingInstruction: case XmlNodeType.XmlDeclaration: newTreeNode.Text = ""; break; case XmlNodeType.Element: newTreeNode.Text = "<" + xmlNode.Name + ">"; break; case XmlNodeType.Attribute: newTreeNode.Text = "ATTRIBUTE: " + xmlNode.Name; break; case XmlNodeType.Text: case XmlNodeType.CDATA: newTreeNode.Text = xmlNode.Value; break; case XmlNodeType.Comment: newTreeNode.Text = ""; break; } // Call this routine recursively for each attribute. // (XmlAttribute is a subclass of XmlNode.) if (xmlNode.Attributes != null) { foreach (XmlAttribute attribute in xmlNode.Attributes) { ConvertXmlNodeToTreeNode(attribute, newTreeNode.Nodes); } } // Call this routine recursively for each child node. // Typically, this child node represents a nested element, // or element content. foreach (XmlNode childNode in xmlNode.ChildNodes) { ConvertXmlNodeToTreeNode(childNode, newTreeNode.Nodes); } } } As an example, consider the following simple XML file (which is included with the sample code as the ProductCatalog.xml file): Code View: Scroll / Show All Jones and Jones Unique Catalog 2004 2005-01-01 Gourmet Coffee The finest beans from rare Chilean plantations. 0.99 true Blue China Tea Pot A trendy update for tea drinkers. 102.99 true

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!