Unknownnode when loading XML in class

Asked

Viewed 31 times

0

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

<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>

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;}
}

Summary is this, but when loading the xml in the class I have the following error:

Unknow Node: ROW

I have changed the structure of the class in several ways, I used attributes to set Elementname and nothing.

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

use in 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);
    }
}
  • How are you doing the parse? Give more details, code is missing

  • edited and added the way I upload xml to the class (object)

  • Sorry but we are still missing the Parameters and Header class to take a look. If possible the error print tb

1 answer

0


Your XML mapping is incorrect. In order 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

Do not use Camel Case for class and property names.

See working on Dotnetfiddle

Reunions:

  1. Capitalization Conventions
  2. Capitalization Styles
  • Perfect! Thank you very much! My interpretation was totally wrong, I was considering ROWS as List and in fact it is not a List, it is just a class, and the ROW that is a List, time I saw your example I felt like hitting my head on the table kkk Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.