473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

Jul 21 '08 #1
6 1727
"mw" <mw@discussions.microsoft.comwrote in message
news:16**********************************@microsof t.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.

Jul 21 '08 #2
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(object sender, EventArgs e)
{
var products = from x in
XElement.Load(@"C:\Temp\test.xml").DescendantsAndS elf("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; }
}
--

Jul 21 '08 #3
No, there is no XSD schema. Thanks for the response however.

Matthew

"Alberto Poblacion" wrote:
"mw" <mw@discussions.microsoft.comwrote in message
news:16**********************************@microsof t.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.

Jul 22 '08 #4
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(object sender, EventArgs e)
{
var products = from x in
XElement.Load(@"C:\Temp\test.xml").DescendantsAndS elf("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; }
}
--

Jul 22 '08 #5
"mw" <mw@discussions.microsoft.comwrote in message
news:26**********************************@microsof t.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").DescendantsAnd Self("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") ...
Jul 22 '08 #6
Thanks again, Ill give it a shot.
"Alberto Poblacion" wrote:
"mw" <mw@discussions.microsoft.comwrote in message
news:26**********************************@microsof t.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").DescendantsAndS elf("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") ...
Jul 23 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Maxim Khesin | last post by:
I want to have a config file with my python proggie, satisfying the following requirements: 1) support key->(value, default) 2) simple and intuitive to read and edit 3) easyly readable into a...
4
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply*...
3
by: Richard Lewis Haggard | last post by:
I have a test application that is calling an assembly that reads some strings out of a config file. Normally, this assembly supports a web application and the information can be read just fine....
13
by: Khodr | last post by:
Hello, I am using VS.NET 2003 and vb. I build my application MyApp and it generates MyApp.exe.config. So now MyApp.exe reads parameters from MyApp.exe.config. Great and no problem! I need to...
20
by: tomerfiliba | last post by:
hey i've been seeing lots of config-file-readers for python. be it ConfigObj (http://www.voidspace.org.uk/python/configobj.html) or the like. seems like a trend to me. i came to this conclusion...
11
by: TARUN | last post by:
Hello All I need to ask about the configuration file in .NET, There are Two config File 1. Web Config 2. Machine config I understand the the usage of Web config , but not able to understand...
12
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
5
by: mmcd79 | last post by:
I built a VB.net application that makes use of a machine level DB connection string setting, and a user level starting location setting. The machine level setting and the default user based...
10
by: eagle | last post by:
I have a web.config in my application that contains the connection strings to all my datasources. I want to move these connection strings to another web config up the folder hierarchy so that all...
5
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a app that uses several membership/role providers. I can list these Providers with the code: Dim rootWebConfig1 As Configuration rootWebConfig1 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.