none
Creating object array from a xml tag

    Question

  • Hi,

    I have simple XML...

    <Change>
     <Product>
      <Type>Change</Type>
       <ID  Key="4367"/>
       <Field Name="Field1">Caffeine-free</Field>
       <Field Name="Field2">24.3</Field>
     </Product>
     <Product>
       <Type>New</Type>
       <ID  Key="4367"/>
       <Field Name="Field1">Caffeinee</Field>
       <Field Name="Field2">24.3</Field>
     </Product>
    </Change>

    Based on XML tags mentioned above, how can i create array of object like below

    is it possible to create array like this...

    I am using LINQ with XML.

    Pleas suggest.

    Thanks in advance


    Tuesday, November 19, 2013 10:47 AM

Answers

  • Thanks for the reply...

    Hope this code snippet will help me to restructure as per my requirement.

    keep you posed, if need  any more help.

    Thanks once again :)

    • Marked as answer by chrasa Thursday, November 21, 2013 9:50 AM
    Thursday, November 21, 2013 9:50 AM

All replies

  • Hi,

    I am afraid that the issue is out of support range of VS General Question forum which mainly discusses WPF & SL designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System and Visual Studio Editor.

    I am moving this thread to XML, System.Xml, MSXML and XmlLite forum for better response.

    Thanks,


    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.
    Click HERE to participate the survey.

    Wednesday, November 20, 2013 8:16 AM
  • Hope this helps as a start ..

     public class Product
        {
        
              public string type;
              public int Key;
              public string Field1;
              public double Field2;
            public Product(string type, int Key, string Field1, double Field2)
            {
                this.type = type;
                this.Key = Key;
                this.Field1 = Field1;
                this.Field2 = Field2;
            }
        }
       class Program
        {
              static void Main(string [] args)
            {
                XElement objDoc = XElement.Load(@"C:\Sites\stringEx\product.xml");
                List<Product> products = new List<Product>();
                //You can use array also but List gives you more flexibility
                //Product [] arrProducts  = new Product[5];
                var productNodes = objDoc.Descendants("Product");
                foreach (XElement el in productNodes)
                {
                    string type = el.Element("Type").Value;
                    int Key = Convert.ToInt32(el.Element("ID").Attribute("Key").Value);
                    string Field1 = el.Elements("Field").ElementAt(0).Value;
                    var Field2 = el.Elements("Field").ElementAt(1).Value;
                    Product p = new Product(type, Key, Field1, Convert.ToDouble(Field2));
                    products.Add(p);
                }
                foreach (var p in products)
                {
                    Console.WriteLine("{0} {1} {2} {3}", p.type, p.Key, p.Field1, p.Field2);
                }
            }
         }

    Wednesday, November 20, 2013 4:50 PM
  • Thanks for the reply...

    Hope this code snippet will help me to restructure as per my requirement.

    keep you posed, if need  any more help.

    Thanks once again :)

    • Marked as answer by chrasa Thursday, November 21, 2013 9:50 AM
    Thursday, November 21, 2013 9:50 AM