question on xPath iterator and Base64 encoding

L

Lonewolf

Hi all,
please pardon me if this question is too trivial. I have an XML file
which stores data in base64. The schema is something like this,
<Remokon>
<Brand Name="SONY">
<Model Name="Type 1", Freq="2">
<Key ID="1" Len="42">
YR0RHhEOEQ0RHiENEg0RDhIMEg0hHRINEgwSDSIcEg0SDBINEg0R/4QA
</Key>
<Key ID="2" Len="38">
YhwSHRIMEg0yLBIMEwwSDRIMIhwTDBINEgwiHCINEhwSDRL/hAA=
</Key>
.....
</Model>
<Model Name="Type 2">
....
</Model>
</Brand>
<Brand Name="Philips>
....
</Brand>
....
</Remokon>

The value of the Key node stores binary data encoded using base64. I can
use .NET's XmlWriter and XmlReader to read and write with no issue. I
use a byte array to receive the decoded binary data, length is specified
by the len attribute of the key note.

As can be see, this seves as some kind of database which I would like to
query using XPath. Herein lies the question, how do I read the value o
fthe key node when I query for all the keys in a Brand's Model? My xpath
query is correct as I tried using string as the value adn it was able to
obtain the keys correctly. the query is something like SELECT all keys
where Brand="SONY" and Model="Type 1". Can someone please enlighten me
on how to use xpath's iterator to retrieve the value stored in Base64
encoded data and decode back into binary data based on the Len attribute
of the Key? I saw the iterator's Current.Value retrieves a string,
ValueAsLong, etc, but sadly no ValueAsBase64. XmlReader has a
ReadAsBase64 function which i used for reading the entire file. But
surely i won't want to use a SELECT * FROM * kind of query on a
database. Please can somebody enlighten me on this? Thank you so much
for your kind assistance on my query.
 
M

Martin Honnen

Lonewolf wrote:

please pardon me if this question is too trivial. I have an XML file
which stores data in base64. The schema is something like this,
<Remokon>
<Brand Name="SONY">
<Model Name="Type 1", Freq="2">
<Key ID="1" Len="42">
YR0RHhEOEQ0RHiENEg0RDhIMEg0hHRINEgwSDSIcEg0SDBINEg0R/4QA
</Key>
<Key ID="2" Len="38">
YhwSHRIMEg0yLBIMEwwSDRIMIhwTDBINEgwiHCINEhwSDRL/hAA=
</Key>
....
</Model>
<Model Name="Type 2">
...
</Model>
</Brand>
<Brand Name="Philips>
...
</Brand>
...
</Remokon>

The value of the Key node stores binary data encoded using base64. I can
use .NET's XmlWriter and XmlReader to read and write with no issue. I
use a byte array to receive the decoded binary data, length is specified
by the len attribute of the key note.

As can be see, this seves as some kind of database which I would like to
query using XPath. Herein lies the question, how do I read the value o
fthe key node when I query for all the keys in a Brand's Model? My xpath
query is correct as I tried using string as the value adn it was able to
obtain the keys correctly. the query is something like SELECT all keys
where Brand="SONY" and Model="Type 1".

Here is a .NET 2.0/C# example that uses the Select method with an
appropriate XPath expression to get a node iterator over the Key
elements, then uses ReadSubtree to get an XmlReader and then applies
ReadElementContentAsBase64:

XPathDocument xPathDocument = new XPathDocument(@"file.xml");
XPathNavigator navigator = xPathDocument.CreateNavigator();
XPathNodeIterator nodeIterator = navigator.Select(
"/Remokon/Brand[@Name = 'SONY']/Model[@Name = 'Type 1']/Key");
while (nodeIterator.MoveNext()) {
using (XmlReader xmlReader = nodeIterator.Current.ReadSubtree()) {
if (xmlReader.Read()) {
byte[] buffer = new byte[2048];
try {
int bytesRead =
xmlReader.ReadElementContentAsBase64(buffer, 0, buffer.Length);
Console.WriteLine("Read {0} bytes.", bytesRead);
}
catch (Exception e) {
Console.WriteLine("Error {0} reading key.", e);
}
}
}
}
 
L

Lonewolf

Martin said:
Lonewolf wrote:

please pardon me if this question is too trivial. I have an XML
file which stores data in base64. The schema is something like this,
<Remokon>
<Brand Name="SONY">
<Model Name="Type 1", Freq="2">
<Key ID="1" Len="42">
YR0RHhEOEQ0RHiENEg0RDhIMEg0hHRINEgwSDSIcEg0SDBINEg0R/4QA
</Key>
<Key ID="2" Len="38">
YhwSHRIMEg0yLBIMEwwSDRIMIhwTDBINEgwiHCINEhwSDRL/hAA=
</Key>
....
</Model>
<Model Name="Type 2">
...
</Model>
</Brand>
<Brand Name="Philips>
...
</Brand>
...
</Remokon>

The value of the Key node stores binary data encoded using base64. I
can use .NET's XmlWriter and XmlReader to read and write with no
issue. I use a byte array to receive the decoded binary data, length
is specified by the len attribute of the key note.

As can be see, this seves as some kind of database which I would like
to query using XPath. Herein lies the question, how do I read the
value o fthe key node when I query for all the keys in a Brand's
Model? My xpath query is correct as I tried using string as the value
adn it was able to obtain the keys correctly. the query is something
like SELECT all keys where Brand="SONY" and Model="Type 1".

Here is a .NET 2.0/C# example that uses the Select method with an
appropriate XPath expression to get a node iterator over the Key
elements, then uses ReadSubtree to get an XmlReader and then applies
ReadElementContentAsBase64:

XPathDocument xPathDocument = new XPathDocument(@"file.xml");
XPathNavigator navigator = xPathDocument.CreateNavigator();
XPathNodeIterator nodeIterator = navigator.Select(
"/Remokon/Brand[@Name = 'SONY']/Model[@Name = 'Type 1']/Key");
while (nodeIterator.MoveNext()) {
using (XmlReader xmlReader = nodeIterator.Current.ReadSubtree()) {
if (xmlReader.Read()) {
byte[] buffer = new byte[2048];
try {
int bytesRead = xmlReader.ReadElementContentAsBase64(buffer,
0, buffer.Length);
Console.WriteLine("Read {0} bytes.", bytesRead);
}
catch (Exception e) {
Console.WriteLine("Error {0} reading key.", e);
}
}
}
}

thanx for your help. It does work. Thank you for enlightening me. :)
 

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

Top