0

I generated a class file from xmltosharp but I don't know how I can use it. I was doing all manually without class, but I will use class to be better in future codes.

I don't know how I can use it with linq, this is to generate a new XML file with complex structure, I need a start foot to follow with my steps.

My Class file as something like this (partial)

/* 
 Licensed under the Apache License, Version 2.0

 http://www.apache.org/licenses/LICENSE-2.0
 */
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{ [XmlRoot(ElementName = "EnviarLoteRpsEnvio", Namespace = "http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_enviar_lote_rps_envio.xsd")]
    public class EnviarLoteRpsEnvio
    {
        [XmlElement(ElementName = "LoteRps", Namespace = "http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_enviar_lote_rps_envio.xsd")]
        public LoteRps LoteRps { get; set; }
        [XmlElement(ElementName = "Signature", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
        public Signature Signature { get; set; }
        [XmlAttribute(AttributeName = "xmlns")]
        public string Xmlns { get; set; }
        [XmlAttribute(AttributeName = "tc", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Tc { get; set; }
    }
}

My form file I'm trying this:

private void xmlbox_Click(object sender, EventArgs e)
        {
            EnviarLoteRpsEnvio el = new EnviarLoteRpsEnvio();
            el.Xmlns = "tc";
            el.LoteRps.CpfCnpj.Cnpj = "09581129000105";


        XDocument xdoc = new XDocument();
        xdoc.Add(new XDeclaration("1.0", "utf-8", "yes"));
        xdoc.Element(el);
        xdoc.Save("teste3.xml");

This is What I'm specting as XML file

<?xml version="1.0" encoding="utf-8"?>
<EnviarLoteRpsEnvio <?xml version="1.0" encoding="utf-8"?>
<EnviarLoteRpsEnvio xmlns="http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_enviar_lote_rps_envio.xsd" xmlns:tc="http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd">
  <LoteRps>
    <tc:NumeroLote>1</tc:NumeroLote>
    <tc:CpfCnpj>
      <tc:Cnpj>09581129000105</tc:Cnpj>
    </tc:CpfCnpj>
    <tc:InscricaoMunicipal></tc:InscricaoMunicipal>
    <tc:QuantidadeRps>1</tc:QuantidadeRps>
    <tc:ListaRps>
      <tc:Rps>
        <tc:InfRps>
          <tc:IdentificacaoRps>
            <tc:Numero>215</tc:Numero>
            <tc:Serie>10</tc:Serie>
            <tc:Tipo>1</tc:Tipo>
          </tc:IdentificacaoRps>
    </tc:Rps>
    </tc:ListaRps>
    </loteRps>
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
Ademir
  • 1
  • 1
  • Are you trying to convert an XML file to a C# class or a C# class to a XML file? – Odrai Mar 26 '19 at 19:51
  • No. I just converted it. I'm trying to use the generated class – Ademir Mar 26 '19 at 20:07
  • I think it is easier to follow the next steps: 1. Copy the XML file content, 2. Create a new C# class. 3. Within Visual Studio navigate to Edit -> Paste Special -> Paste XML as Classes. (or you could use the described 'xmltosharp') 4. Use a XmlSerializer to Serialize objects to a XML file and Deserialize the file to an object (in this case EnviarLoteRpsEnvio). Pleae have a look at the next page as well: [link](https://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object-to-from-file) – Odrai Mar 26 '19 at 20:18
  • Download the msdn tool xsd.exe which will automatically convert the schema to classes. The URL namesspace in your xml is the top level schema.(xsd file). In the top level namespace there are additional namesaces (URL xsd files) that you have to also convert. – jdweng Mar 27 '19 at 09:04

0 Answers0