C# - How to write binary byte array to CData property in XML - c#

I have an XML that implements from IXmlSerializable. I want to add a CData property so I can add binary data to the property in the XML. I'm going to pass the XML along and in another application use the binary data.
I know some characters won't be able to translate certain characters but ignoring that fact, how would I achieve this? I tried several ways from stackoverflow but I have been unsuccessful.
[XmlElementAttribute(ElementName = "test", Form = XmlSchemaForm.Unqualified)]
[XmlElement("CDataElement")]
public RawXml test
{
get
{
return test;
}
set
{
test= value;
}
}
byte[] bAry= BinaryData;
item.Property= new CustomXML(bAry);
"item" and "CustomXML" both derive from IXmlSerializable.

XML is a text based container. You cannot place binary data within a text based container without transforming it to some kind of character based translation.
Most engines will take the binary and encode it as base64 implicitly.
C# XmlWriter has the method XmlWriter.WriteBase64.
Of course you can use Convert.ToBase64String() in order to translate this yourself and pass it in as any other string value.
It should not be necessary to think about this at all...
And just to mention: No need for a CDATA section here. CDATA is something you do not need at all and which should be avoided...

You'll have to do:
Convert.ToBase64String(yourBinaryData) and pass it as a string in CData, then at the other end you'll want to use: Convert.FromBase64String(yourCDataText)
If you really wanted to send binary data over XML you could do:
<DATA>
<BINARY>
<BIT index="0">0</BIT>
<BIT index="1">0</BIT>
...
<BIT index="99">1</BIT>
</BINARY>
</DATA>
However you're better off just doing base64 encoding.

Related

How to escape invalid characters inside XML string in C#

I have an XML string in C#. This XML has several tags. In some of these tags there are invalid characters like '&' in the text. I need to escape these characters inside the text from the whole long XML string but I want to keep the tags.
I have tried HttpUtility.HtmlEncode and few other available methods but they encode the whole string rather then just the text inside the tags. Example tags are
<node1>This is a string & so is this</node1> should be converted to
<node1>This is a string & so is this</node1>
Any ideas? thanks
P.S. I know similar question has been asked before I have not found a complete solution for this problem.
I guess the simplest solution is to load the whole Xml document in memory as an XmlDocument and then go through the elements and replace the values with their html encoded form.
you can use a CDATA field, like this:
<YourXml>
<Id>1</Id>
<Content>
<![CDATA[
your special caracteres
]]>
</content>
</yourXml>
I dont get what is the big deal in this. When you have the entire xml as a string, the easiest way to achieve what u want is to use the Replace function.
For example the whole xml is in the string str, then all u have to do is,
str.Replace("&" , "&");
Thats it man. You have achieved whatever u wanted to. Some times very simple solutions exist for big problems. Hope this helps for you.
XDocument or XmlDocument is a way to go. If for some crazy out of your control reason you need to encode just text blocks inside XmlElement:
using System.Text;
using System.Xml;
static string EncodeText(string unescapedText) {
if (string.IsNullOrEmpty(unescapedText)) {
return unescapedText;
}
var builder = new StringBuilder(unescapedText.Length);
using (var writer = XmlTextWriter.Create(builder, new XmlWriterSettings {
ConformanceLevel = ConformanceLevel.Fragment
})) {
writer.WriteValue(unescapedText);
}
return builder.ToString();
}

Validate or parse a string for XML?

I have a Description textbox on the page. When enter the data in that and submit the page. I will pass that string to XML tag in the XML file.
If user enter any invalid characters in textbox which are not allowed for xml. How to remove or parse them from string? I need to validate string for XML data.
If you're using the XmlDocument or XDocument classes to build the XML then you don't need to worry as they'll do the encoding for you.
Otherwise, if you generating the XML by hand you can use the SecurityElement.Escape method to encode invalid XML characters
That depends on how you are creating the XML. If you are assembling the XML string yourself, there are A LOT of things you should do and take into consideration.
Thus, you should not be doing that (assembling the string yourself).
.NET provides you with abstraction layers so you don't have to deal with that. Example: XDocument

Handling \x01 received from Flash's ExternalInterface

I'm receiving data from a Flash component embedded in a Windows Form. Unfortunately, if the data returned from the socket contains any of the following characters, the call to loadXml below fails:
This is the callback method I have to receive data from the socket (via ExternalInterface in the Flash component).
private void player_FlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
{
String output = e.request;
//output = CleanInvalidXmlChars(output);
XmlDocument document = new XmlDocument();
document.LoadXml(output);
XmlAttributeCollection attributes = document.FirstChild.Attributes;
String command = attributes.Item(0).InnerText;
XmlNodeList list = document.GetElementsByTagName("arguments");
process(list[0].InnerText);
I had a method to replace the characters with text (CleanInvalidXmlChars), but I don't think this is the right approach.
How can I load this data into an XML file, as this makes separating the method name, paramter names and parameter types which are returned very easy to work with.
Would appreciate any help at all.
Thanks.
If the “XML” contains any U+0001 (aka '\x01') or other similar characters, it is not a valid XML. There is no way you can include those characters in XML (well, in XML 1.0, anyway). See the XML specification. If you need to pass e.g. binary data in XML, you need to convert them to a proper form, e.g. using Base-64.
If the data does contain those invalid characters, it is not XML, and therefore cannot be read using standard XML tools (I don’t think any of the standard .NET classes allows you to override that behavior). You can either replace all those characters (these are basically all control characters (U+0000 through U+001F) except U+0009 (tab), U+000A and U+000D (CR+LF), plus U+FFFE and U+FFFF (noncharacters)) prior to use as you tried – you could devise a safe transformation which would not lose any data (e.g. first replace all # characters with #0040, then replace any invalid character with #xxxx where xxxx is its code, and when processing the parsed XML data, replace all #xxxx back).
Another option is to drop the XML idea and just process it as a string. Just for inspiration, see e.g. this piece of code.

Is there a XmlReader method similar to the XmlWriter.WriteRaw method?

XmlWriter.WriteRaw will preserve &apos; and not send an actual apostrophe. Is there a method to read in &apos; and keep it as such?
You need to encode it properly. Let's take for example the following XML:
<root>&apos;</root>
The value of the <root> node is ' no matter which XML parser you use to read this XML.
On the other hand if you have the following XML:
<root>&apos;</root>
the value of the <root> node is &apos;.
So in both cases we have properly encoded XML so that when a standard compliant parser reads it, it is able to correctly retrieve the value.
So be very careful when using the WriteRaw method when generating the XML. Since it properly encode the argument it is now your responsibility to ensure that you are passing correct data to it.

what is the best way to parse out CDATA inside XML tags

i have the following XML that i need to parse in C#:
<data>
<customer_id><![CDATA[1414301]]></customer_id>
<last_modified_date><![CDATA[2011-08-14 11:58:58]]></last_modified_date>
<customer_first_name><![CDATA[joe]]></customer_first_name>
<customer_last_name><![CDATA[blow]]></customer_last_name>
</data>
into an object called Result
public class Result
{
public int customer_id;
public string customer_first_name;
public string customer_last_name;
}
the one thing that i am trying to determine is how to get rid of the
<![CDATA[]]
and just parse out the regular values.
What is the best way of converting this XML to the object above that will support the CDATA syntax above
Not sure what you mean by 'parse'. If you use an XML DOM parser (or any other type), then you have transparent access to the CDATA values.
Using System.XML? Try XmlCDataSection?

Categories

Resources