UnknownNode when loading XML into class

0

I get the following XML from a database (modified data, but the structure is this)

<registro>
  <header>
    <codUsuario>XXX</codUsuario>
    <codLojista>999</codLojista>
  </header>
  <parametros>
    <consulta>XXX</consulta>
  </parametros>
  <ROWS>
    <ROW>
      <produto>1</produto>
      <tab>10</tab>
    </ROW>
    <ROW>
      <produto>2</produto>
      <tab>10</tab>
    </ROW>
  </ROWS>
</registro>

I wrote a class:

public class registro
{
  public Header header { get; set; }
  public Parametros parametros { get; set;}
  public List<Row> ROWS { get; set;}
}

public class Row
{
  public string produto { get; set;}
  public string tab { get; set;}
}

public class Parametros
{
  public string consulta { get; set;}
}

public class Header
{
  public string codUsuario { get; set;}
  public string codLojista { get; set;}
}

Unknow node: ROW

I have already altered the class structure in several ways, used attributes to set the ElementName and nothing.

EDIT: I load the XML into the class using a resource I use to load other XML documents, including NF-e / CT-e / NFS-e

use this way:

registro reg = new registro();
ReadXML<registro>(ref reg, xml);

ReadXML method:

public void ReadXML<T>(ref T AObject, string AXml)
{
    try
    {
        byte[] encodedString = Encoding.UTF8.GetBytes(AXml);
        MemoryStream ms = new MemoryStream(encodedString);
        ms.Flush();
        ms.Position = 0;

        XmlSerializer ser = new XmlSerializer(typeof(T));
        ser.UnknownAttribute += new XmlAttributeEventHandler(Serializer_UnknownAttribute);
        ser.UnknownElement += new XmlElementEventHandler(Serializer_UnknownElement);
        ser.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
        ser.UnreferencedObject += new UnreferencedObjectEventHandler(Serializer_UnreferencedObject);

        AObject = (T)ser.Deserialize(ms);
    }
    catch (Exception ex)
    {
        throw new Exception("Erro ao serializar. Erro: " + ex.Message);
    }
}
    
asked by anonymous 05.03.2018 / 21:45

1 answer

0

The mapping of your XML is incorrect. For the code to work as expected, you need to use a wrapper that encapsulates the list.

Classes

[XmlRoot(ElementName="header")]
public class Header {
    [XmlElement(ElementName="codUsuario")]
    public string CodUsuario { get; set; }
    [XmlElement(ElementName="codLojista")]
    public string CodLojista { get; set; }
}

[XmlRoot(ElementName="parametros")]
public class Parametros {
    [XmlElement(ElementName="consulta")]
    public string Consulta { get; set; }
}

[XmlRoot(ElementName="ROW")]
public class Row {
    [XmlElement(ElementName="produto")]
    public string Produto { get; set; }
    [XmlElement(ElementName="tab")]
    public string Tab { get; set; }
}

[XmlRoot(ElementName="ROWS")]
public class Rows {
    [XmlElement(ElementName="ROW")]
    public List<Row> Row { get; set; }
}

[XmlRoot(ElementName="registro")]
public class Registro {
    [XmlElement(ElementName="header")]
    public Header Header { get; set; }
    [XmlElement(ElementName="parametros")]
    public Parametros Parametros { get; set; }
    [XmlElement(ElementName="ROWS")]
    public Rows Rows { get; set; }
}

Capitalization Conventions

It is not recommended to use Camel Case for class names and properties.

See working at DotNetFiddle

References:

  • Capitalization Conventions
  • Capitalization Styles
  • 05.03.2018 / 22:15