0

I have multiple XML files as string in C# and i want to convert this string to C# Array. I am trying through xmldocument but it throughs following exception. "Data at the root level is invalid. Line 463, position 1."

XmlDocument doc = new XmlDocument();
doc.LoadXml(context.Request.Params[2]); 

Please help me how can i convert multiple xml files (string) to c# array.

ARC
  • 1,061
  • 14
  • 33
  • 2
    Error is self explanatory. Please show your xml – Gilad Green Aug 17 '17 at 05:59
  • @GiladGreen due to sensitive data i can't show that. – ARC Aug 17 '17 at 06:01
  • Then we can't help. Your xml is not valid in line Line 463. your code is fine. – rene Aug 17 '17 at 06:01
  • Then how can we help you? Show a small section on it and remove the sensitive data from it – Gilad Green Aug 17 '17 at 06:01
  • The line number in error is where the second file is starting – ARC Aug 17 '17 at 06:05
  • you have multiple files in the same big string? – Gilad Green Aug 17 '17 at 06:06
  • @GiladGreen yes – ARC Aug 17 '17 at 06:07
  • Wouldn't splitting the string with "end of file" and loading each xml separately better ? – Rafalon Aug 17 '17 at 06:10
  • 1
    Why hold multiple files in same variable. Have an array of them and in each a separate file. Without seeing better how the data is kept I cannot give a proper solution.. but problem is because they are together – Gilad Green Aug 17 '17 at 06:10
  • @Rafalon i am sending multiple files in an array from front end that is becoming cumulative string in c#. should i split it and then call the loadxml method or any other solution ? – ARC Aug 17 '17 at 06:13
  • 1
    Yes, I think you should find what char or what string is added between each file and split the whole string by this char or string (or try to get it in an array at the beginning, maybe with json ?). – Rafalon Aug 17 '17 at 06:23
  • You shouldn't concatenate multiple XML file contents into a single string because the string now contains multiple XML [root elements](https://en.wikipedia.org/wiki/Root_element), which is not allowed by the [XML standard](https://www.w3.org/TR/2006/REC-xml-20060816/#sec-well-formed). Instead, load each separately. If you must somehow parse such a concatenated string, see [C# XDocument Load with multiple roots](https://stackoverflow.com/q/18186225). – dbc Aug 17 '17 at 08:33
  • You xml file is not "Well Formed". You can sitll read file using XmlReader() method by setting the following : settings.ConformanceLevel = ConformanceLevel.Fragment;. I would like to see the xml file before posting any code. – jdweng Aug 17 '17 at 09:44
  • Actually file is too large and have confidential data, that's why not possible to remove all the data and post here. – ARC Aug 17 '17 at 10:25

0 Answers0