S
Simon Harris
Hi All,
I am trying to set properties of a class from the values of an XML
document.
The document is structured like this:
<?xml version="1.0" encoding="utf-16" standalone="yes" ?>
- <Page>
<Title>Test</Title>
<MetaKeywords>Test</MetaKeywords>
<MetaDescription>Test</MetaDescription>
<Content>Test</Content>
- <Audit>
<LastUpdateBy />
<LastUpdateDate>08/05/2005 09:25:32</LastUpdateDate>
</Audit>
</Page>
The code I have used is:
Me.XMLFileLocation = strXMLFileLocation
Dim XMLFileStream As New FileStream(strXMLFileLocation, FileMode.Open)
Dim XMLReader As New XmlTextReader(XMLFileStream)
XMLReader.MoveToContent()
XMLReader.Read()
XMLReader.Read()
Me.Title = XMLReader.Value
XMLReader.Close()
XMLFileStream.Close()
Note the multiple .read() to skip to the desired element. Obviously this
is
far from elegant, as if any change to the structure of the XML will make
this fail.
What I would really like to do is have my code look for an element by
name,
then assign its value to my properties.
Thanks,
Simon
I am trying to set properties of a class from the values of an XML
document.
The document is structured like this:
<?xml version="1.0" encoding="utf-16" standalone="yes" ?>
- <Page>
<Title>Test</Title>
<MetaKeywords>Test</MetaKeywords>
<MetaDescription>Test</MetaDescription>
<Content>Test</Content>
- <Audit>
<LastUpdateBy />
<LastUpdateDate>08/05/2005 09:25:32</LastUpdateDate>
</Audit>
</Page>
The code I have used is:
Me.XMLFileLocation = strXMLFileLocation
Dim XMLFileStream As New FileStream(strXMLFileLocation, FileMode.Open)
Dim XMLReader As New XmlTextReader(XMLFileStream)
XMLReader.MoveToContent()
XMLReader.Read()
XMLReader.Read()
Me.Title = XMLReader.Value
XMLReader.Close()
XMLFileStream.Close()
Note the multiple .read() to skip to the desired element. Obviously this
is
far from elegant, as if any change to the structure of the XML will make
this fail.
What I would really like to do is have my code look for an element by
name,
then assign its value to my properties.
Thanks,
Simon