XML Document with BASE64 Encoded Sections

G

Guest

I have an xml document that contains some elements encoded as Base64. How do
I dynamically scan the XML Document and pull out the sections that are
Base64...

My overall goal is to display the XML document in a browser will all the
Base64 sections converted to Ascii (UTF-8).
 
M

Martin Honnen

Chris said:
I have an xml document that contains some elements encoded as Base64.

What does that mean, there are elements that have contents that is
Base64 encoded?
Or what are "elements encoded as Base64"?
How do
I dynamically scan the XML Document and pull out the sections that are
Base64...

Is there some indication in the document or in a schema for the document
that an element has base64 encoded contents e.g. an attribute indicating
the type perhaps
<data xsi:type="xs:base64Binary">...</data>

Or do you know the tag names of the elements containing base64 encoded data?

You could access the InnerText of such an element and use the method
Convert.FromBase64String
<http://msdn.microsoft.com/library/d...rfSystemConvertClassFromBase64StringTopic.asp>
to convert the text content to a byte array.
 
G

Guest

Yes, the XML document does indicate which elemets are Base64 encoded as such:
<xsd:element name="Payload" type="xsd:base64Binary"/>

I do know the tag names now, but would like to make it more flexible since
additional tag names in the future may be added that would break the design.

My main challenge is to dynamically find the elements that are Base64 and
decode them to Ascii (UTF-8). I just need help on finding these sections.
 
M

Martin Honnen

Chris said:
Yes, the XML document does indicate which elemets are Base64 encoded as such:
<xsd:element name="Payload" type="xsd:base64Binary"/>

That looks like a schema definition. A schema is usually external to the
XML instance document e.g. your XML instance document is then more
likely to have
I just need help on finding these sections.

There are various APIs in the .NET framework to read out information
from an XML document, there is XmlTextReader which is a fast forward
only pull parsing approach that does not consume much memory but
requires you to set up your own code to extract data and store it.
There is XPathDocument which loads a complete document in memory into an
optimized data structure for read only XPath navigation and data access.
And there is XmlDocument which also loads the complete document in
memory but in a structure which allows manipulation. XmlDocument also
allows XPath navigation.
 
G

Guest

Martin,

So without the xml fragment containing an attribute that describes it's
type, I cannot dynamically determine the sections that are base64? I was
thinking that the definition of the type in the schema would be sufficient?
The XML doc contains a reference to the schema, so I would think the solution
would be to scan the xsd for base64 types and then pull out these sections
from the xml fragment and convert to ascii. Your suggestions?
 
M

Martin Honnen

Chris Fink wrote:

So without the xml fragment containing an attribute that describes it's
type, I cannot dynamically determine the sections that are base64? I was
thinking that the definition of the type in the schema would be sufficient?
The XML doc contains a reference to the schema, so I would think the solution
would be to scan the xsd for base64 types and then pull out these sections
from the xml fragment and convert to ascii.

If the schema and the instance are available then you can use an
XmlValidatingReader and access type informations from the schema while
parsing the XML instance document. The following snippet checks for
elements with bas64Binary typed contents and reads out the content into
a byte array then:

XmlValidatingReader validator = new XmlValidatingReader(new
XmlTextReader(@"test2005102001.xml"));
validator.ValidationType = ValidationType.Schema;
validator.ValidationEventHandler += new
ValidationEventHandler(ValidationHandler);

while (validator.Read()) {
if (validator.NodeType == XmlNodeType.Element) {
if (validator.SchemaType is XmlSchemaDatatype) {
XmlSchemaDatatype currentType =
(XmlSchemaDatatype)validator.SchemaType;
if (currentType.ToString() ==
"System.Xml.Schema.Datatype_base64Binary") {
Console.WriteLine("Element {0} has base64Binary type.",
validator.Name);
object currentValue = validator.ReadTypedValue();
Console.WriteLine("Typed value read as {0}.",
currentValue.GetType().Name);
// could use currentValue as byte[] here
}
}
}
}

validator.Close();
 
G

Guest

Exactly what I was looking for.

Thank you very much!

Martin Honnen said:
Chris Fink wrote:

So without the xml fragment containing an attribute that describes it's
type, I cannot dynamically determine the sections that are base64? I was
thinking that the definition of the type in the schema would be sufficient?
The XML doc contains a reference to the schema, so I would think the solution
would be to scan the xsd for base64 types and then pull out these sections
from the xml fragment and convert to ascii.

If the schema and the instance are available then you can use an
XmlValidatingReader and access type informations from the schema while
parsing the XML instance document. The following snippet checks for
elements with bas64Binary typed contents and reads out the content into
a byte array then:

XmlValidatingReader validator = new XmlValidatingReader(new
XmlTextReader(@"test2005102001.xml"));
validator.ValidationType = ValidationType.Schema;
validator.ValidationEventHandler += new
ValidationEventHandler(ValidationHandler);

while (validator.Read()) {
if (validator.NodeType == XmlNodeType.Element) {
if (validator.SchemaType is XmlSchemaDatatype) {
XmlSchemaDatatype currentType =
(XmlSchemaDatatype)validator.SchemaType;
if (currentType.ToString() ==
"System.Xml.Schema.Datatype_base64Binary") {
Console.WriteLine("Element {0} has base64Binary type.",
validator.Name);
object currentValue = validator.ReadTypedValue();
Console.WriteLine("Typed value read as {0}.",
currentValue.GetType().Name);
// could use currentValue as byte[] here
}
}
}
}

validator.Close();
 

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