Monday, May 05, 2008
If your project is leveraging a Web service and you need more data to start out with, here is what you can do:

1) Write code in your device application that calls the addX (X is your thing ... restaurant, task, etc.) of your Web service. Make sure you provide unique IDs!

2) Create a network location (see previous blog) and work directly in the XML. Open your XML file in Visual Studio. Can be a quick way, but also error prone, which may lead to problems in the deserialization of your XML data. Use copy/paste as much as possible.

3) Modify the generateData method in your Web service. Right now it only creates one test item. Simply repeat as much as you like the code that creates the data item, assigns the values, and adds it to the array list. Make sure you provide unique IDs!
        Dim al As New ArrayList()
        Dim da As DataItem()
        Dim di As DataItem

        Try
            'test item (your item may have other attributes!)
            di = New DataItem
            di.ID = "0000" 'provide unique ids
            di.Name = "test"
            di.Value = 1.0
            al.Add(di)

            'another item
            di = New DataItem
            di.ID = "0001"
            di.Name = "test again"
            di.Value = 1.0
            al.Add(di)

            '... and so on

            da = al.ToArray(GetType(DataItem))

            Return DataManagementSystem.saveDataItems(da)
        Catch ex As Exception
            Return ex.Message
        End Try



I would use 3 to get started (and reset if necessary) and then work using 2.

MH

5/5/2008 12:07:06 PM (Central Standard Time, UTC-06:00)
 Wednesday, April 30, 2008

For anyone you needs to do some direct XML processing (i.e., RSS feeds) take a look at the following past blog entry and other resources:

Blog post about Google Calendar feed

Slides with XML processing code

... or just use this RSS Feed Web service :-) (Just plug-in the RSS URL, it returns an RssFeed object with its "news" items. The application key is "rss".)

MH


Technorati : , ,

4/30/2008 3:34:07 PM (Central Standard Time, UTC-06:00)
 Friday, April 04, 2008

How can one leverage XML without knowing much about XML?

The answer, as indicated in the previous posting, is XML serialization.

An XML serializer can be used to turn the data stored in objects into XML (and back from XML into objects). All the serializer needs to know is the class the objects are derived from. The serializer looks at the class definition and creates a corresponding XML structure. (The class has to be "serializable" and there are some cases where this automatic process will not work!)

Below is some example code in which an XML serializer is leveraged to save and load DataItem objects. The loadDataItems function reads the XML from a file and returns an array of DataItem objects. (DataItem could be replaced with Person, Patient, Contract, Restaurant, ... whatever your class is!). The saveDataItems function takes an array of DataItem objects and writes it to an XML file.


MH

--- Code ---

'need these imports at the top of your VB code!
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO

...

Public Shared Function loadDataItems() As DataItem()
Dim mySerializer As XmlSerializer
Dim dataItems As DataItem()

Try
mySerializer = New XmlSerializer(GetType(DataItem()))

' To read the file, create a FileStream.
Dim myFileStream As FileStream = _
New FileStream(_filePath, FileMode.Open)

' Call the Deserialize method and cast to the object type.
dataItems = CType(mySerializer.Deserialize(myFileStream), DataItem())
Return dataItems
Catch ex As Exception
Return Nothing
End Try

End Function

Public Shared Function saveDataItems(ByVal dataItems As DataItem()) As Boolean
Dim mySerializer As XmlSerializer

Try
mySerializer = New XmlSerializer(GetType(DataItem()))

' To write to a file, create a StreamWriter object.
Dim myWriter As StreamWriter = _
New StreamWriter(_filePath)

mySerializer.Serialize(myWriter, dataItems)
myWriter.Close()
Return True
Catch ex As Exception
Return False
End Try

End Function

project | XML
4/4/2008 9:22:02 AM (Central Standard Time, UTC-06:00)
Search
Navigation
On this page....
Archives
<September 2008>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
Aggregate Me!
RSS 2.0 | Atom 1.0 | CDF
Categories
Blogroll
Contact me
Send mail to the author(s) E-mail
Themes
Administration