×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    Problem in parsing embedded Xml file

    I have included a xml file in my project as a resource. The "Build Action" property of that xml file is set to "Resource".

    For Deserailzation, I am using the code as:
    Code:
        public partial class MainPage : PhoneApplicationPage
        {
            StreamResourceInfo xml;
            // Constructor
            public MainPage()
            {
                InitializeComponent();
                xml = Application.GetResourceStream(new Uri("MyProjectNameSpace;component/MyXmlFile.xml", UriKind.Relative));
                if (xml != null)
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Beams));
                    Beams beams = (Beams)serializer.Deserialize(xml.Stream);
                    lstBeans.ItemsSource = beams.Collection;
                    int hh = lstBeans.Items.Count(); // for testing.. comes out 0             
                }
             }
    I am unable to see "beams " value in the form of a string. If I try to check it using ToString() then the value I get is "MyProjectNameSpace.Beams". So, I don't know the cause of the problem whether its due to Deserialize() or lack of using Parse().

    The "Beams.cs" class is as below:
    Code:
    namespace MyProjectNameSpace
    {
        [XmlRoot("root")]
        public class Beams
        {       
            [XmlArray("beams")]
            [XmlArrayItem("beam")]
            public ObservableCollection<Beam> Collection { get; set; }
        }
    }
    The "Beam.cs" class is as follows:
    Code:
      public class Beam
        {
            [XmlElement("ID")]
            public string ID { get; set; }
    
            [XmlElement("placeName")]
            public string placeName { get; set; }
    
            [XmlElement("location")]
            public string location { get; set; }
    
            [XmlElement("desc")]
            public string desc { get; set; }
    
            [XmlElement("nearestAirport")]
            public string nearestAirport { get; set; }
    
            [XmlElement("railAvailable")]
            public string railAvailable { get; set; }
    
            [XmlElement("latitude")]
            public string latitude { get; set; }
    
            [XmlElement("longitude")]
            public string longitude { get; set; }
        }
    Please suggest me something for the solution.
    Last edited by pooja_1650; 2012-03-18 at 08:17.

  2. #2
    Registered User
    Join Date
    Jan 2012
    Location
    Kochin
    Posts
    10

    Re: Problem in parsing embedded Xml file

    Little confusing your beam class you have added two beam.cs code which one is your actual code Beams or Pilgrimage..?
    If possible please include xml file also.

  3. #3
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    Re: Problem in parsing embedded Xml file

    Quote Originally Posted by dheeraj_pk@hotmail.com View Post
    Little confusing your beam class you have added two beam.cs code which one is your actual code Beams or Pilgrimage..?
    If possible please include xml file also.
    Corrected the Beam class name. Also, the xml file contents are:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <root>
    <Beams>
    <Beam>
    <ID>101</ID>
    <placeName>A</placeName>
    <location>Germany</location>
    <desc>first</desc>
    <nearestAirport>zooma</nearestAirport>
    <railAvailable>Yes</railAvailable>
    <latitude>34.02960</latitude>
    <longitude>75.94830</longitude>
    </Beam>
    <Beam>
    <ID>102</ID>
    <placeName>B</placeName>
    <location>Germany</location>
    <desc>second</desc>
    <nearestAirport>zooma</nearestAirport>
    <railAvailable>Yes</railAvailable>
    <latitude>26.80000</latitude>
    <longitude>82.20000</longitude>
    </Beam>
    <Beam>
    <ID>103</ID>
    <placeName>C</placeName>
    <location>Germany</location>
    <desc>third</desc>
    <nearestAirport>zooma</nearestAirport>
    <railAvailable>Yes</railAvailable>
    <latitude>30.73333</latitude>
    <longitude>79.48333</longitude>
    </Beam>
    <Beam>
    <ID>104</ID>
    <placeName>D</placeName>
    <location>Germany</location>
    <desc>gofor</desc>
    <nearestAirport>zooma</nearestAirport>
    <railAvailable>Yes</railAvailable>
    <latitude>27.58333</latitude>
    <longitude>77.70000</longitude>
    </Beam>
    <Beam>
    <ID>105</ID>
    <placeName>E</placeName>
    <location>Germany</location>
    <desc>amazing</desc>
    <nearestAirport></nearestAirport>
    <railAvailable>Yes</railAvailable>
    <latitude>30.98333</latitude>
    <longitude>78.93333</longitude>
    </Beam>
    <Beam>
    <ID>106</ID>
    <placeName>F</placeName>
    <location>Germany</location>
    <desc>gofor</desc>
    <nearestAirport></nearestAirport>
    <railAvailable>Yes</railAvailable>
    <latitude>29.95611</latitude>
    <longitude>78.17000</longitude>
    </Beam>
    <Beam>
    <ID>107</ID>
    <placeName>G</placeName>
    <location>Germany</location>
    <desc>gotta</desc>
    <nearestAirport></nearestAirport>
    <railAvailable>Yes</railAvailable>
    <latitude>30.70022</latitude>
    <longitude>79.61598</longitude>
    </Beam>
    <Beam>
    <ID>108</ID>
    <placeName>H</placeName>
    <location>Germany</location>
    <desc>godds</desc>
    <nearestAirport>bonmdkd</nearestAirport>
    <railAvailable>Yes</railAvailable>
    <latitude>30.73333</latitude>
    <longitude>79.06667</longitude>
    </Beam>
    </Beams>
    </root>

  4. #4
    Registered User
    Join Date
    Jan 2012
    Location
    Kochin
    Posts
    10

    Re: Problem in parsing embedded Xml file

    Case mistake.. change beam as follow
    [XmlArray("beams")]
    [XmlArrayItem("beam")]
    to
    [XmlArray("Beams")]
    [XmlArrayItem("Beam")]


    [XmlRoot("root")]
    public class Beams
    {
    [XmlArray("Beams")]
    [XmlArrayItem("Beam")]
    public ObservableCollection<Beam> Collection { get; set; }
    }

  5. #5
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    Re: Problem in parsing embedded Xml file

    Corrected that but still getting same result.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Location
    Kochin
    Posts
    10

    Re: Problem in parsing embedded Xml file

    Please try the code below
    private void ParseXML()
    {
    //use correct URI..
    var xmlStream= App.GetResourceStream(new Uri("Beam.xml", UriKind.Relative));
    if (xmlStream != null)
    {
    XmlSerializer serializer = new XmlSerializer(typeof(Beams));
    Beams beams = (Beams)serializer.Deserialize(xmlStream.Stream);

    var itemcollection = beams.Collection;
    }
    }



    public class Beam
    {
    [XmlElement("ID")]
    public string ID { get; set; }

    [XmlElement("placeName")]
    public string placeName { get; set; }

    [XmlElement("location")]
    public string location { get; set; }

    [XmlElement("desc")]
    public string desc { get; set; }

    [XmlElement("nearestAirport")]
    public string nearestAirport { get; set; }

    [XmlElement("railAvailable")]
    public string railAvailable { get; set; }

    [XmlElement("latitude")]
    public string latitude { get; set; }

    [XmlElement("longitude")]
    public string longitude { get; set; }
    }

    [XmlRoot("root")]
    public class Beams
    {
    [XmlArray("Beams")]
    [XmlArrayItem("Beam")]
    public ObservableCollection<Beam> Collection { get; set; }
    }

  7. #7
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    Re: Problem in parsing embedded Xml file

    Tried but still getting "There is an error in XML document (2, 2)." at below line:
    Code:
    Beams beams = (Beams)serializer.Deserialize(xmlStream.Stream);

  8. #8
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    Re: Problem in parsing embedded Xml file

    Finally.. solved it. Thank you Dheeraj for your suggestions.
    The problem was in .xml file. The nodes text was different than defined in Beam.cs class (case sensitive problem).

  9. #9
    Registered User
    Join Date
    Jan 2012
    Location
    Kochin
    Posts
    10

    Re: Problem in parsing embedded Xml file

    k good. keep going..

Similar Threads

  1. problem in parsing XML file
    By jayesh4456 in forum Symbian
    Replies: 9
    Last Post: 2012-01-10, 10:44
  2. Installation problem of CameraWrapper embedded sis file
    By djscorpion83 in forum Symbian Signed Support, Application Packaging and Distribution and Security
    Replies: 1
    Last Post: 2009-08-20, 08:23
  3. Problem with embedded sis file
    By nadav70 in forum Symbian
    Replies: 3
    Last Post: 2008-01-29, 15:30
  4. problem in parsing xml file using syexpat
    By sunny_singh in forum Symbian
    Replies: 21
    Last Post: 2006-07-17, 11:56

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •