XML config

  • Thread starter Thread starter mw
  • Start date Start date
M

mw

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
 
mw said:
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.
 
mw said:
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(object sender, EventArgs e)
{
var products = from x in
XElement.Load(@"C:\Temp\test.xml").DescendantsAndSelf("flavor")
select new Product
{
flavor = (string)x.Attribute("id"),
size = (string)x.Element("size"),
brand = (string)x.Element("brand"),
price = (string)x.Element("price")
};
foreach (Product p in products)
{
ListViewItem item = new ListViewItem(new 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; }
}


--
 
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 said:
mw said:
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(object sender, EventArgs e)
{
var products = from x in
XElement.Load(@"C:\Temp\test.xml").DescendantsAndSelf("flavor")
select new Product
{
flavor = (string)x.Attribute("id"),
size = (string)x.Element("size"),
brand = (string)x.Element("brand"),
price = (string)x.Element("price")
};
foreach (Product p in products)
{
ListViewItem item = new ListViewItem(new 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; }
}
 
mw said:
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").DescendantsAndSelf("flavor")
[...]

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(myXmlDocument.InnerXml)
XElement xe = XElement.Load(sr);
var products = from x in xe.DescendantsAndSelf("flavor") ...
 
Thanks again, Ill give it a shot.


Alberto Poblacion said:
mw said:
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").DescendantsAndSelf("flavor")
[...]

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(myXmlDocument.InnerXml)
XElement xe = XElement.Load(sr);
var products = from x in xe.DescendantsAndSelf("flavor") ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top