XML

  • Thread starter Thread starter David A. Osborn
  • Start date Start date
D

David A. Osborn

Currently I have an XML file that come from an external program that looks
something like this:

<?xml version="1.0" ?><XML_FRUITBOWL_DATA_FILE FILEVERSION="1"
APPVERSION="19">
<FRUITBOWL Name="bowl1" Size="6" >
<FRUIT Type="Apple" Quanity="1" Size="Big"
Brand="Red Del." >
</FRUIT>
<FRUIT Type="Orange" Quanity="2" Size="small"
Brand="none">
</FRUIT>
</FRUIT BOWL>
<FRUITBOWL Name="bowl2" Size="24" >
<FRUIT Type="Apple" Quanity="16" Size="small"
Brand="Other" >
</FRUIT>
<FRUIT Type="Grape" Quanity="2" Size="medium"
Brand="none">
</FRUIT>
</FRUIT BOWL>
</XML_FRUITBOWL_DATA_FILE>


At any time the external program could change and a new property could be
added. What is the best way for me to interact with this file? Can I read
it in as a generic object and access properties with the dot notation? Or
do I need to interact with the file with SQL statement with ADO .NET?
 
August 9, 2005

There are MANY ways you can read that file.... ADO.Net & SQL would be
complicated for such a simple task. Just look into Dataset.ReadXML &
XPathNavigators & XMLDocuments.... easy ways to retrieve data from XML
Files.... if the file changes, I would use a FileMonitor class (I hope that
is the exact name of it... I forget, maybe FileWatcher?) which will fire an
event that you can use to read the new file when it changes... I hope this
helps and have a great day!

--
Joseph Bittman
Microsoft Certified Solution Developer

Web Site: http://71.39.42.23
Static IP
 
What I meant by the file changing was that the the file format may change,
ie another property may be added so I don't want to have to rewrite this
code every time the vendor of the software updates their software. What
would be nice is if I could pull the XML in as an object that automatically
had the properties that were listed in the XML, but I don't even know if
that is possible. I think I am sort of looking for the opposite of a
serialized class, if that makes any sense. Turn the XML into an object
without actually knowing what that object is.
 
what's more important at this point is to determine what your application is
doing with the file...or any new properties. are you just trying to find a
way to better visually display (for readability sake) the xml?


| What I meant by the file changing was that the the file format may change,
| ie another property may be added so I don't want to have to rewrite this
| code every time the vendor of the software updates their software. What
| would be nice is if I could pull the XML in as an object that
automatically
| had the properties that were listed in the XML, but I don't even know if
| that is possible. I think I am sort of looking for the opposite of a
| serialized class, if that makes any sense. Turn the XML into an object
| without actually knowing what that object is.
|
|
| | > August 9, 2005
| >
| > There are MANY ways you can read that file.... ADO.Net & SQL would be
| > complicated for such a simple task. Just look into Dataset.ReadXML &
| > XPathNavigators & XMLDocuments.... easy ways to retrieve data from XML
| > Files.... if the file changes, I would use a FileMonitor class (I hope
| > that is the exact name of it... I forget, maybe FileWatcher?) which will
| > fire an event that you can use to read the new file when it changes... I
| > hope this helps and have a great day!
| >
| > --
| > Joseph Bittman
| > Microsoft Certified Solution Developer
| >
| > Web Site: http://71.39.42.23
| > Static IP
| >
| >
| >
| >
| > | >> Currently I have an XML file that come from an external program that
| >> looks something like this:
| >>
| >> <?xml version="1.0" ?><XML_FRUITBOWL_DATA_FILE FILEVERSION="1"
| >> APPVERSION="19">
| >> <FRUITBOWL Name="bowl1" Size="6" >
| >> <FRUIT Type="Apple" Quanity="1" Size="Big"
| >> Brand="Red Del." >
| >> </FRUIT>
| >> <FRUIT Type="Orange" Quanity="2" Size="small"
| >> Brand="none">
| >> </FRUIT>
| >> </FRUIT BOWL>
| >> <FRUITBOWL Name="bowl2" Size="24" >
| >> <FRUIT Type="Apple" Quanity="16" Size="small"
| >> Brand="Other" >
| >> </FRUIT>
| >> <FRUIT Type="Grape" Quanity="2" Size="medium"
| >> Brand="none">
| >> </FRUIT>
| >> </FRUIT BOWL>
| >> </XML_FRUITBOWL_DATA_FILE>
| >>
| >>
| >> At any time the external program could change and a new property could
be
| >> added. What is the best way for me to interact with this file? Can I
| >> read it in as a generic object and access properties with the dot
| >> notation? Or do I need to interact with the file with SQL statement
with
| >> ADO .NET?
| >>
| >>
| >>
| >>
| >>
| >
| >
| >
|
|
|
 
Hello David,

You can deserialize into an object, assuming that you have a schema for the
file. I wish that the schema editor in Visual Studio was easier to work
with, but it isn't. That said, you can use the XSD tool in the SDK (free
download) to create the schema for you. (see
http://msdn.microsoft.com/library/d...s/html/cpconXMLSchemaDefinitionToolXsdexe.asp )
[watch for wrapping URLs].

Once you have the schema generated from the XSD, load it into your project.
Make sure that the elements are defined as "open" and not closed (I don't
know which is the default for the XSD tool).

Then save your changes and run the updated schema back through the XSD tool
to generate the classes you can deserialize your data into. Now you will be
able to read your data using normal "dot" notation.

HTH,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Basically what I need to do is update specific fields in the file. For
example I may need to update the quanity for each fruit in a fruit bowl. If
the external vendor added another property I just need to be ble to retain
it in the XML, either f my output is the same XML file or if I generate a
new XML file. It sounds to me that interfacing with SQL might be the best
solution since I just need to update specific fields. What would the SQL
statement look like to grab the quanity of APPLE in BOWL1?
 

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