Parsing the xml file not parsing correctly.

phaniphani USMember
protected void GetItems ()
        {
            string strPath = "http://abc.com/Maintenance.asmx/GetItemsTest";

            NSData dataReturned = NSData.FromUrl (NSUrl.FromString (strPath));

            string newstring = dataReturned.ToString();
            Console.WriteLine("the value is : {0}",newstring); //check output below

            Parser parser = new Parser();
            parser.ParseItem(newstring);
        }


public void ParseItem (string xmlfile)
        {
            XmlDocument doc= new XmlDocument();
            doc.LoadXml(xmlfile);
            doc.Save(@"item.xml");

            ItemsCollection itemCollection = new ItemsCollection();

            XPathDocument xpd = new XPathDocument (@"item.xml");
            XPathNavigator xpn = xpd.CreateNavigator ();
            XPathNodeIterator xpi = xpn.Select ("/ArrayOfNItems/NItems");

            while (xpi.MoveNext()) 
            {
                Item item = new Item ();
                XPathNodeIterator childElement = xpi.Current.SelectChildren(XPathNodeType.Element);

                while(childElement.MoveNext())
                {
                    if (childElement.Current.Name == "NItemID")
                    {
                        item.ItemID = Convert.ToInt32(childElement.Current.Value);
                    }
                    else if(childElement.Current.Name=="NItem")
                    {
                        item.ItemName = childElement.Current.Value;
                    }
                }
                //item.items.Add(item);
                itemCollection.items.Add(item);
            }
            Console.WriteLine("items totally are : {0}",itemCollection.items.Count);
        }

My output for the value is

 the value is : <?xml version="1.0" encoding="utf-8"?>
<ArrayOfNItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <NItems>
    <NItemID>1</NItemID>
    <NItem>A/C</NItem>
  </NItems>
  <NItems>
    <NItemID>2</NItemID>
    <NItem>Blinds</NItem>
  </NItems>
  <NItems>
    <NItemID>3</NItemID>
    <NItem>Carpet1</NItem>
  </NItems>
  <NItems>
    <NItemID>4</NItemID>
    <NItem>Carpet2</NItem>
  </NItems>
  <NItems>
    <NItemID>5</NItemID>
    <NItem>Doors</NItem>
  </NItems>
</ArrayOfNItems>

Console.WriteLine("items totally are : {0}",itemCollection.items.Count);
First is it correct to save the string to xml and like that doc.save..?
The item count is always 0. What am i doing wrong?

Posts

  • josemiguel.torresjosemiguel.torres ESXamarin Team Xamurai

    Try:

    `public void ParseItem (string xmlfile)
    {
    XmlDocument doc= new XmlDocument();
    doc.LoadXml(xmlfile);
    doc.Save(@item.xml);

            ItemsCollection itemCollection = new ItemsCollection();
    
            XPathDocument xpd = new XPathDocument (@"item.xml");
            XPathNavigator xpn = xpd.CreateNavigator ();
            XPathNodeIterator xpi = xpn.Select ("/ArrayOfNItems/NItems");
    // add this
     xpi.MoveNext();
     XPathNavigator xpiNavigator = xpi.Current;
     XPathNodeIterator nodeIterator = xpiNavigator.SelectDescendants(XPathNodeType.Text, false);
    
    // end
    
            //while (xpi.MoveNext()) 
            while (nodeIterator.MoveNext())
            {
                Item item = new Item ();
                XPathNodeIterator childElement = xpi.Current.SelectChildren(XPathNodeType.Element);
    
                while(childElement.MoveNext())
                {
                    if (childElement.Current.Name == "NItemID")
                    {
                        item.ItemID = Convert.ToInt32(childElement.Current.Value);
                    }
                    else if(childElement.Current.Name=="NItem")
                    {
                        item.ItemName = childElement.Current.Value;
                    }
                }
                //item.items.Add(item);
                itemCollection.items.Add(item);
            }
            Console.WriteLine("items totally are : {0}",itemCollection.items.Count);
        }`
    

    and let us know,

    regards,

  • phaniphani USMember

    @Jose Miguel. Thanks. I figured out another way to approach parsing. I chose linq to xml. That worked. But will try this approach on other project.

Sign In or Register to comment.