1

I have a file .xml inside multiple xml in one line. How can I read this file and convert to object? I tried with this code it works if there is only one. Please help and thank you all

[XmlRoot(ElementName = "DepartmentMaster")]
public class DepartmentMaster
{
    [XmlElement(ElementName = "DepartmentId")]
    public int DepartmentId { get; set; }

    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }

    [XmlElement(ElementName = "Description")]
    public string Description { get; set; }

    [XmlElement(ElementName = "test")]
    public int Test { get; set; }
}

//string xml = "<DepartmentMaster><DepartmentId>267854</DepartmentId><Name>Purchase</Name><Description>Purchase Department</Description><test>1</test></DepartmentMaster>";

 string xml = "<DepartmentMaster><DepartmentId>267854</DepartmentId><Name>Purchase</Name><Description>Purchase Department</Description><test>1</test></DepartmentMaster><DepartmentMaster><DepartmentId>267855</DepartmentId><Name>Purchase5</Name><Description>Purchase Department5</Description><test>5</test></DepartmentMaster>";

using (TextReader reader = new StringReader(xml))
{
    System.Xml.Serialization.XmlSerializer deserializer = new System.Xml.Serialization.XmlSerializer(typeof(DepartmentMaster));
    var model = (DepartmentMaster)deserializer.Deserialize(reader);
}

image from the database

image from the database

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
forme forme
  • 43
  • 1
  • 6
  • 3
    This is not a valid XML document. An XML document must have a single root element. You could try to wrap the string into "...". – Klaus Gütter Sep 11 '21 at 14:01
  • It is a table in the database. It is written in the same way. I am trying to upload a picture with the subject. But it does not appear – forme forme Sep 11 '21 at 14:09
  • 1
    If you want to use XmlSerializer you need to start with valid XML-you need a root element. – Tu deschizi eu inchid Sep 11 '21 at 14:13
  • I have added a link to image of the table in the database – forme forme Sep 11 '21 at 14:14
  • It is not necessary to use XmlSerializer. Is there a solution to this and thank you I'm a beginner – forme forme Sep 11 '21 at 14:17
  • 1
    The XML specification allow an array of tags at root. Do not listen to others that say different. When you have an array at root it is not "Well Formed", but is valid. There are two solutions. 1) Add a root to the string. string wellFormed = "" + xml + "; 2) The Net library does like the well formed data. So you need to use a XmlReader with a XmlReaderSetting like this : XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; – jdweng Sep 11 '21 at 15:25
  • Might you please [edit] your question to include your code and JSON as **text** rather than as a screenshot? It's policy here not to to use images for textual data, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. A [mcve] with both code and XML would increase your chances of getting an answer, for why see [ask]. – dbc Sep 11 '21 at 15:48
  • But from the image it seems like your XML file is not well-formed because it has multiple [roots](https://en.wikipedia.org/wiki/Root_element). If so you can parse such a file by setting [`XmlReaderSettings.ConformanceLevel = ConformanceLevel.Fragment`](https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlreadersettings.conformancelevel), and then deserialize to a `List` as shown in [this answer](https://stackoverflow.com/a/46476652/3744182) to [Read nodes of a xml file in C#](https://stackoverflow.com/q/46476119/3744182). In fact I think this is a duplicate, agree? – dbc Sep 11 '21 at 15:51
  • Or if you would prefer to read into a list of `XElement` or `XmlDocument` objects see [this answer](https://stackoverflow.com/a/18203952/3744182) to [C# XDocument Load with multiple roots](https://stackoverflow.com/q/18186225/3744182) by [Martin Honnen](https://stackoverflow.com/users/252228/martin-honnen). I'd say your question is a duplicate of that or the previously linked question. – dbc Sep 11 '21 at 15:54

1 Answers1

1

Here it is two approaches below.

The first is using setting to accept XML data with multiple root elements (ConformanceLevel.Fragment).

private static IList<DepartmentMaster> DeserializeFragment(string xml)
{
    var settings = new XmlReaderSettings
    {
        ConformanceLevel = ConformanceLevel.Fragment
    };
    
    XmlReader reader = XmlReader.Create(new MemoryStream(Encoding.ASCII.GetBytes(xml)), settings);
    var serializer = new XmlSerializer(typeof(DepartmentMaster));
    
    var list = new List<DepartmentMaster>();
    while (serializer.Deserialize(reader) is DepartmentMaster element)
    {
        list.Add(element);
    }
    return list;
}

And the second by adding a root element to deserialize a well-formed XML document.

public class DepartmentMasters
{
    [XmlElement("DepartmentMaster")]
    public List<DepartmentMaster> Items;
}

private static DepartmentMasters DeserializeWellFormedXML(string xml)
{
    var text = @"<?xml version=""1.0""?><DepartmentMasters>" + xml + "</DepartmentMasters>";
    var serializer = new XmlSerializer(typeof(DepartmentMasters));
    return (DepartmentMasters)serializer.Deserialize(new StringReader(text));
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • The link is for serializing, not deserializing. The code will not work at a root unless you use the Fragment setting as I stated in main thread. – jdweng Sep 11 '21 at 15:28