I am trying to get xml string from an attachment using IMAPX which later saves the data to byte[]. and after conversion, the string has linebreaks represented by \\r\\n.
byte[] att = w.FileData;
str = System.Text.Encoding.Default.GetString(att);
the string seems to have "\\r\\n" breaks instead of whitespace and new lines
str = str.Replace("\r\n", " ");
when I try to read this Xml using a string reader to read the Xml
StringReader sr = new StringReader("your xml");
DataSet ds = new DataSet();
ds.ReadXml(sr);
it doesn't seem that the program can understand the code in such format. I have tried to apply linebreaks in the Xml instead of \\r\\n like using 
 ; instead but without any success. the Xml works fine and converts to a datatable if it was retrieved from a file on the system but I want to be able to create the datatable from the string directly
original xml:
<?xml version="1.0" standalone="yes"?>
<Data>
<ID>931304</ID>
<Age>26</Age>
</Data>
<Data>
<ID>932160</ID>
<Age>26</Age>
</Data>
<Data />
it becomes like this on reading the byte[] att to a string
<?xml version="1.0" standalone="yes"?> \r\n <DocumentElement> \r\n <Data> \r\n <ID>931304</ID> \r\n <Age>26</Age> \r\n </Data> \r\n <Data> \r\n <ID>932160</ID> \r\n <Age>26</Age> \r\n </Data> \r\n <Data /> \r\n </DocumentElement>
the ds.ReadXml(sr);
does not recognize the \\r\\n, and even if it was replaced with a blank space.
You can not transfer any xml into datatable, Your xml is wrong for convert.
<?xml version="1.0" standalone="yes"?> <-- why "?" this at the start and end.
...<Data /> at the end
Either you remove the first line and last line, which is valid xml , then it convert into datatable
or
You can use linq for this typeof data.
Loading XML String into datatable
or
You can use loadxml
method and then loop for each node like
How to display DataTable XML from WriteXml in a nicely readable format?
Updated or you can use schema for this.
First create a schema - ( How to convert complex XML to .NET Class? )
then just parse the xml to schema and convert into datatable.
The DataSet.ReadXml
method accepts file name as string ( Documentation ). You cannot pass the xml itself as string. You have to convert the string into stream to do that.
You'll have to do something like this:
StringReader sr = new StringReader("your xml");
DataSet ds = new DataSet();
ds.ReadXml(sr);
I have managed to create the datatable from a string although it wasn't readable by
StringReader sreader = new StringReader(string);
DataTable.ReadXml(sreader);
the trick is to feed the DataTable with the schema of your data
Data.ReadXmlSchema("yourschema.xsd");
without constructing the table in the program then feeding the data using the StringReader() in ReadXml() as above;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.