XML config

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?bXc=?=

    XML config

    Hi,

    I'd like to know if there is a way that I could create a new object for each
    product in an XML file.


    XML----------

    <flavor id="vanilla">
    <size>300ml</size>
    <brand>coles</brand>
    <prince>12.00 </price>
    </flavor>

    ------------

    for instance, create a new class

    public class Product
    {
    public string flavor
    public string size
    public string brand
    public string price
    }

    So that after importing the XML, you could use the info like so:

    vanilla.size;

    Bit of a weird example, but its all I could think of, I must be hungry. Its
    actually for a config file.

    Thanks, I've been trying to get this to work all day, and I'm sure its quite
    simple.


    Matthew

  • Alberto Poblacion

    #2
    Re: XML config

    "mw" <mw@discussions .microsoft.comw rote in message
    news:16858190-64A6-4D1F-A589-1782A8535BA1@mi crosoft.com...
    I'd like to know if there is a way that I could create a new object for
    each
    product in an XML file.
    >
    >
    XML----------
    >
    <flavor id="vanilla">
    <size>300ml</size>
    <brand>coles</brand>
    <prince>12.00 </price>
    </flavor>
    >
    ------------
    >
    for instance, create a new class
    >
    public class Product
    {
    public string flavor
    public string size
    public string brand
    public string price
    }
    Do you know beforehand the structure of the XML, or does it need to be
    dynamically discovered at runtime?

    If you know the structure of the XML, and you have it documented as an
    XSD schema, then you can use the XSD.EXE program that comes with Visual
    Studio to generate a class like the one you want. The class is created with
    the adequate Attributes so that if you use the XmlSerializer to export or
    import the contents of the class, it produces or reads an XML file that is
    compliant with your original XSD schema.

    Comment

    • Tim Jarvis

      #3
      Re: XML config

      mw wrote:
      Hi,
      >
      I'd like to know if there is a way that I could create a new object
      for each product in an XML file.
      >
      >
      XML----------
      >
      <flavor id="vanilla">
      <size>300ml</size>
      <brand>coles</brand>
      <prince>12.00 </price>
      </flavor>
      >
      ------------
      >
      for instance, create a new class
      >
      public class Product
      {
      public string flavor
      public string size
      public string brand
      public string price
      }
      >
      So that after importing the XML, you could use the info like so:
      >
      vanilla.size;
      >
      Bit of a weird example, but its all I could think of, I must be
      hungry. Its actually for a config file.
      >
      Thanks, I've been trying to get this to work all day, and I'm sure
      its quite simple.
      >
      >
      Matthew

      Hi Matthew,

      Try something like this :-)

      private void button1_Click(o bject sender, EventArgs e)
      {
      var products = from x in
      XElement.Load(@ "C:\Temp\test.x ml").Descendant sAndSelf("flavo r")
      select new Product
      {
      flavor = (string)x.Attri bute("id"),
      size = (string)x.Eleme nt("size"),
      brand = (string)x.Eleme nt("brand"),
      price = (string)x.Eleme nt("price")
      };
      foreach (Product p in products)
      {
      ListViewItem item = new ListViewItem(ne w string[] { p.flavor,
      p.size, p.brand, p.price });
      listView1.Items .Add(item);
      }

      }

      }

      public class Product
      {
      public string flavor { get; set; }
      public string size { get; set; }
      public string brand { get; set; }
      public string price { get; set; }
      }


      --

      Comment

      • =?Utf-8?B?bXc=?=

        #4
        Re: XML config

        No, there is no XSD schema. Thanks for the response however.

        Matthew

        "Alberto Poblacion" wrote:
        "mw" <mw@discussions .microsoft.comw rote in message
        news:16858190-64A6-4D1F-A589-1782A8535BA1@mi crosoft.com...
        I'd like to know if there is a way that I could create a new object for
        each
        product in an XML file.


        XML----------

        <flavor id="vanilla">
        <size>300ml</size>
        <brand>coles</brand>
        <prince>12.00 </price>
        </flavor>

        ------------

        for instance, create a new class

        public class Product
        {
        public string flavor
        public string size
        public string brand
        public string price
        }
        >
        Do you know beforehand the structure of the XML, or does it need to be
        dynamically discovered at runtime?
        >
        If you know the structure of the XML, and you have it documented as an
        XSD schema, then you can use the XSD.EXE program that comes with Visual
        Studio to generate a class like the one you want. The class is created with
        the adequate Attributes so that if you use the XmlSerializer to export or
        import the contents of the class, it produces or reads an XML file that is
        compliant with your original XSD schema.
        >
        >

        Comment

        • =?Utf-8?B?bXc=?=

          #5
          Re: XML config

          Thanks very much for you response Tim, that might work, however the rest of
          my XML is being imported using XmlDocument. I definately should have
          mentioned that in my initial post. Any further suggestions?

          Thanks,

          Matthew



          "Tim Jarvis" wrote:
          mw wrote:
          >
          Hi,

          I'd like to know if there is a way that I could create a new object
          for each product in an XML file.


          XML----------

          <flavor id="vanilla">
          <size>300ml</size>
          <brand>coles</brand>
          <prince>12.00 </price>
          </flavor>

          ------------

          for instance, create a new class

          public class Product
          {
          public string flavor
          public string size
          public string brand
          public string price
          }

          So that after importing the XML, you could use the info like so:

          vanilla.size;

          Bit of a weird example, but its all I could think of, I must be
          hungry. Its actually for a config file.

          Thanks, I've been trying to get this to work all day, and I'm sure
          its quite simple.


          Matthew
          >
          >
          Hi Matthew,
          >
          Try something like this :-)
          >
          private void button1_Click(o bject sender, EventArgs e)
          {
          var products = from x in
          XElement.Load(@ "C:\Temp\test.x ml").Descendant sAndSelf("flavo r")
          select new Product
          {
          flavor = (string)x.Attri bute("id"),
          size = (string)x.Eleme nt("size"),
          brand = (string)x.Eleme nt("brand"),
          price = (string)x.Eleme nt("price")
          };
          foreach (Product p in products)
          {
          ListViewItem item = new ListViewItem(ne w string[] { p.flavor,
          p.size, p.brand, p.price });
          listView1.Items .Add(item);
          }
          >
          }
          >
          }
          >
          public class Product
          {
          public string flavor { get; set; }
          public string size { get; set; }
          public string brand { get; set; }
          public string price { get; set; }
          }
          >
          >
          --
          >
          >

          Comment

          • Alberto Poblacion

            #6
            Re: XML config

            "mw" <mw@discussions .microsoft.comw rote in message
            news:26E7D371-4C48-447C-8C80-616C7E02410C@mi crosoft.com...
            Thanks very much for you response Tim, that might work, however the rest
            of
            my XML is being imported using XmlDocument. I definately should have
            mentioned that in my initial post. Any further suggestions?
            [...]
            > var products = from x in
            >XElement.Load( @"C:\Temp\test. xml").Descendan tsAndSelf("flav or")
            >[...]
            You should still be able to use the above method. It only requires that
            you have your XML inside of an XElement. Even though the above example
            loaded the XElement from a file, the Load method of XElement has an overload
            that loads the xml from a TextReader. You can easily build a TextReader from
            the InnerXml property of your XmlDocument:

            StringReader sr = new StringReader(my XmlDocument.Inn erXml)
            XElement xe = XElement.Load(s r);
            var products = from x in xe.DescendantsA ndSelf("flavor" ) ...


            Comment

            • =?Utf-8?B?bXc=?=

              #7
              Re: XML config

              Thanks again, Ill give it a shot.


              "Alberto Poblacion" wrote:
              "mw" <mw@discussions .microsoft.comw rote in message
              news:26E7D371-4C48-447C-8C80-616C7E02410C@mi crosoft.com...
              Thanks very much for you response Tim, that might work, however the rest
              of
              my XML is being imported using XmlDocument. I definately should have
              mentioned that in my initial post. Any further suggestions?
              [...]
              var products = from x in
              XElement.Load(@ "C:\Temp\test.x ml").Descendant sAndSelf("flavo r")
              [...]
              >
              You should still be able to use the above method. It only requires that
              you have your XML inside of an XElement. Even though the above example
              loaded the XElement from a file, the Load method of XElement has an overload
              that loads the xml from a TextReader. You can easily build a TextReader from
              the InnerXml property of your XmlDocument:
              >
              StringReader sr = new StringReader(my XmlDocument.Inn erXml)
              XElement xe = XElement.Load(s r);
              var products = from x in xe.DescendantsA ndSelf("flavor" ) ...
              >
              >
              >

              Comment

              Working...