locked
Unable to use XmlNode

    Question

  • Hi, 

    Every example I have seen online for loading data from an XML file into a combobox looks something like the following: 

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(path);
                XmlNodeList itemList = doc.SelectNodes("ConfigFile/ImagingCIS/title");
                foreach (XmlNode dataSources in itemList)
                {
                    ItemHDSizeUnit.Items.Add(dataSources.Attributes["item"].Value.ToString());
                }

    Whenever I try to use XmlNode, I get an error that the namespace cannot be found. I have the line using System.Xml at the top of my .cs file. Is there a different way I should be attempting to load the data? 

    Thanks.

    Also, if it helps, here is my XML file. If I should change the structure of it at all to make my life easier, let me know. I'm not very experienced with all of this yet. 

    <ConfigFile>
      <ComputerType>
        <title>Computer_Type</title>
    	<item>Desktop</item>
    	<item>Notebook/Laptop</item>
    	<item>Server</item>
    	<item>Disk Array/Raid</item>
    	<item>Single Disk</item>
    	<item>Other</item>
      </ComputerType>
    
      <FileSize>
        <title>File_Size</title>
    	<item>B</item>
    	<item>KB</item>
    	<item>MB</item>
    	<item>GB</item>
    	<item>TB</item>
      </FileSize>
    </ConfigFile>
    Thanks a lot for your help!

    Thursday, July 10, 2014 8:20 PM

Answers

  • Hi,

    XmlNode Class cannot use in windows store app.You can get items value by use the code below:

     public MainPage()
            {
                this.InitializeComponent();
                string peopleXMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "Assets/XMLFile1.xml");
                XDocument loadedData = XDocument.Load(peopleXMLPath);
                var data = from query in loadedData.Descendants("ComputerType")
                           select new ComputerType 
                           {
                               title = (string)query.Element("title"),
                               
                               item = (IEnumerable<XElement>)query.Elements("item")
                              
                           };
                var tt = data.ToList();
                var items = tt[0].item.ToList();
                List<string> values = new List<string>();
                foreach (var item in items){
                
                    values.Add(item.Value.ToString());
                }
    
               
                    
                
                  
            }
        }
        public class ComputerType
        {
            public string title { get; set; }
            public IEnumerable<XElement> item { get; set; }
           /* public ComputerType(string title, IEnumerable<string> item)
            {
                this.title = title;
                this.items = item.ToList();
            }*/
               
        }

    The values contain all items in ComputerType.

    Also you can refer to the link below:

    http://www.geekchamp.com/articles/how-to-read-xml-files-in-windows-8---winrt

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    • Proposed as answer by Dave SmitsMVP Monday, July 14, 2014 5:57 AM
    • Marked as answer by JB-CI-IRS Tuesday, July 15, 2014 1:32 PM
    Friday, July 11, 2014 2:48 AM

All replies

  • Hi,

    XmlNode Class cannot use in windows store app.You can get items value by use the code below:

     public MainPage()
            {
                this.InitializeComponent();
                string peopleXMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "Assets/XMLFile1.xml");
                XDocument loadedData = XDocument.Load(peopleXMLPath);
                var data = from query in loadedData.Descendants("ComputerType")
                           select new ComputerType 
                           {
                               title = (string)query.Element("title"),
                               
                               item = (IEnumerable<XElement>)query.Elements("item")
                              
                           };
                var tt = data.ToList();
                var items = tt[0].item.ToList();
                List<string> values = new List<string>();
                foreach (var item in items){
                
                    values.Add(item.Value.ToString());
                }
    
               
                    
                
                  
            }
        }
        public class ComputerType
        {
            public string title { get; set; }
            public IEnumerable<XElement> item { get; set; }
           /* public ComputerType(string title, IEnumerable<string> item)
            {
                this.title = title;
                this.items = item.ToList();
            }*/
               
        }

    The values contain all items in ComputerType.

    Also you can refer to the link below:

    http://www.geekchamp.com/articles/how-to-read-xml-files-in-windows-8---winrt

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    • Proposed as answer by Dave SmitsMVP Monday, July 14, 2014 5:57 AM
    • Marked as answer by JB-CI-IRS Tuesday, July 15, 2014 1:32 PM
    Friday, July 11, 2014 2:48 AM
  • Thanks a lot for your help! This is perfect.
    Tuesday, July 15, 2014 1:32 PM