0

I have an XML and I load it in an class.

This is my XML

<out_policySystem xmlns:msl="http://www.ibm.com/xmlmap" xmlns:io="" xmlns:xs4xs="http://www.w3.org/2001/XMLSchema">
  <BGBAResultadoOperacion>
    <Severidad>OK</Severidad>
  </BGBAResultadoOperacion>
  <permiteOperar>true</permiteOperar>
  <Lista xmlns:ns0=\"http://CalypsoPolicySystem_lib/service\">
    <Codigo>ODM-006</Codigo>
    <Descripcion>Aviso</Descripcion>
    <DescripcionTecnica>XXXX</DescripcionTecnica>
  </Lista>
</out_policySystem>

I have define my classes like this.

    [XmlRoot(ElementName = "out_policySystem")]
    public partial class output_policySystem
    {
        public BGBAResultadoOperacion BGBAResultadoOperacion { get; set; }
        public bool permiteOperar { get; set; }
       public List[] Lista { get; set; }
    }
      public partial class BGBAResultadoOperacion
    {
        public string Severidad { get; set; }
    }
     public partial class List
    {
        public string Codigo { get; set; }
        public string Descripcion { get; set; }
        public string DescripcionTecnica { get; set; }
    }

I read this like this.

 XmlNodeList elemlist = xDoc.GetElementsByTagName("out_policySystem");
 string result = elemlist[0].InnerXml;
 XmlSerializer serializer = new XmlSerializer(typeof(BGBAResultadoOperacion));

 using (StringReader reader = new StringReader(result))
 {
 result = (BGBAResultadoOperacion)(serializer.Deserialize(reader));
 }

the value of result is this.

<BGBAResultadoOperacion><Severidad>OK</Severidad></BGBAResultadoOperacion><permiteOperar>true</permiteOperar><Lista><Codigo>ODM-006</Codigo><Descripcion>Aviso</Descripcion><DescripcionTecnica>xxxx</DescripcionTecnica></Lista>

What I need is to get the value of BGBAResultadoOperacion

when i set

using (StringReader reader = new StringReader(result))
     {
     result = (BGBAResultadoOperacion)(serializer.Deserialize(reader));
     }

result get XML error...

There are multiple root elements. Line 1, position 76.

XML node out_policySystem has three root elements inside it. I need to parse only BGBAResultadoOperacion

How can I get it?

Thanks

3
  • What is the stack trace of the exception? Commented Aug 27, 2018 at 18:12
  • I add inner exception... it says There are multiple root elements. Line 1, position 76. thanks Commented Aug 27, 2018 at 18:35
  • You need to read the entire xml. The xml serialize needs a well formed xml file which means you can only have one root element. You are removing the element out_policySystem which is the cause of the error which then gives three elements at the root. Commented Aug 27, 2018 at 20:04

1 Answer 1

1

That's because of this line:

elemlist[0].InnerXml

Which returns an XML Fragment, not an XML Document.

  <BGBAResultadoOperacion>
    <Severidad>OK</Severidad>
  </BGBAResultadoOperacion>
  <permiteOperar>true</permiteOperar>
  <Lista xmlns:ns0=\"http://CalypsoPolicySystem_lib/service\">
    <Codigo>ODM-006</Codigo>
    <Descripcion>Aviso</Descripcion>
    <DescripcionTecnica>XXXX</DescripcionTecnica>
  </Lista>

So either use the .OuterXML, or use XElement.CreateReader() as described in the answer here: Serialize an object to XElement and Deserialize it in memory

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.