Extracting a byte[] from XML File

flashkid10

Member
Joined
Dec 9, 2016
Messages
16
Programming Experience
Beginner
I currently have a system in a which I store an image to a byte[] (cover) in a class instance. I'm trying to extract a byte[] from the class instance, but I don't know how to do that.

the code I'm using is

List<Item> information;

information = (from e in XDocument.Load("AniListData.xml").Root.Elements("Item")
               select new Item
                      {
                          Name = (string)e.Element("Name"),
                          Age = (int)e.Element("Age"),
                          Cover = // What do i put here to extract the byte[]
                      }).ToList();
 
Which string representation do you use to store the byte array? Often byte arrays are stored as Base64 strings in xml, and Convert class has convenient methods for doing so.
 
I Don't know what you mean by string representation, but I store the array in a public byte[] in the class Item and I save it to the XML by a method which contains

public static void SaveData(List<Item> obj, string filename)
{
XmlSerializer sr = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, obj);
writer.Close();
}
 
Have you tried just doing exactly what you're doing for the other properties? If you simply use:
Cover = (byte[])e.Element("Cover")

what happens? I can only really think of two possibilities:

1. It will work as is.
2. It will throw an exception.

In the case of #2, the error message will give you a clue as to what you have to do. If it says that you can't cast a string as a byte array then you know that you need to perform the conversion yourself. As JohnH says, the data is likely encoded using base-64, so you can try decoding it that way as a first effort:
Cover = Convert.FromBase64String((string)e.Element("Cover"))
 
It throws a exception in both cases

Exception thrown: 'System.ArgumentNullException' in mscorlib.dll
Additional information: Value cannot be null.
 
That suggests that "Cover" is not valid in that context. Unlike me, you don't have to make assumptions based on limited information. Use whatever name is appropriate in that instance.
 
I Don't know what you mean by string representation,
Everything in a xml file is text, and data that is not text have to be converted to text before written to xml.
but I store the array in a public byte[] in the class Item and I save it to the XML by a method which contains

public static void SaveData(List<Item> obj, string filename)
{
XmlSerializer sr = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, obj);
writer.Close();
}
XmlSerializer will convert byte array data to a Base64 string. Example:
HTML:
<example>aGVsbG8gc2VyaWFsaXphdGlvbg==</example>
If there is no array (null) the element is not written to xml, and if there is an empty array it will write an element that has no data.
HTML:
<example />
 
Back
Top Bottom