Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to everybody.

I'm developing a Visual Studio 2017 (Visual Basic) project trying to generate a XML File to be validated by the proper DAC, for an electronic Invoice.

- I'm using the xmlWriter class, which I used in the past, thru Visual Studio 2010 and it worked just fine.

But now, using the same statements I used previously, when I try to use the 'WriteStartElement' method, it fails and sends the message:

"Carácter de nombre no válido en 'cfdi:Comprobante'. El carácter ':', con valor hexadecimal 0x3A, no puede incluirse en un nombre."

In english (more or less) "Invalid character in name. The character ':' with hexadecimal value 0x3A cannot be included in a name".

- I also used to use the ascii character (34) to concatenate a '"' character in a text string that should have it included as part of the literal, but it generates a couple of '"', not just one, as i want. Like this:

Dim Cadena As String = ":xsi=" + Chr(34) + "http://www.w3.org/2001/XMLSchema-instance" + Chr(34) + " xsi:schemaLocation = " + Chr(34) + "http://www.uif.shcp.gob.mx/recepcion/ari ari.xsd" + Chr(34) _
+ " xmlns = " + Chr(34) + "http://www.uif.shcp.gob.mx/recepcion/ari"

Can anybody help me or give a hint about where to look for this pair of doubts ?

Thanks in advance.

What I have tried:

I´d been looking thru the Internet and I haven't found anything yet. I'm going to continue searching for something, but I'd appreciate any help.

I don't know why the same routines worked fine before and not now.
Posted
Updated 22-Jan-18 9:45am
Comments
David_Wimbley 22-Jan-18 13:34pm    
Can you post some code samples that would be most helpful, you've posted one variable but the whole xml writing that you are attempting to do would be best.
Miguel Altamirano Morales 22-Jan-18 16:29pm    
Thanks for replying, David.

Here'e the entire sub:

Private Sub Genera_XML()
Dim Cadena As String
Cadena = Chr(39) + "http://www.sat.gob.mx/cfd/3" + Chr(39) + "xmlns:xsi = " + Chr(39) + "http//www.w3.org/2001/XMLSchema-instance" + Chr(39) + "xsi:schemaLocation = " + Chr(39) + "http://www.sat.gob.mx/cfd/3 " + Chr(39) + "http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd" + Chr(39)
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
FileNameXML = strPath + "XMLFacturas\" + IDFiscal + ".XML"
On Error GoTo msg_error
Using writer As XmlWriter = XmlWriter.Create(FileNameXML, settings)
' ' Begin writing.
writer.WriteProcessingInstruction("xml", "version='1.0' encoding='utf-8'")
writer.WriteStartElement("cfdi:Comprobante")
writer.WriteElementString("Version", "3.3")
writer.WriteElementString("Serie", Mid(IDFiscal, 1, 3))
writer.WriteElementString("Folio", Mid(IDFiscal, 4, Len(IDFiscal) - 3))
writer.WriteElementString("Fecha", Date.Today.Hour)
writer.WriteEndElement()
' End document.
writer.WriteEndDocument()
End Using
Exit Sub
msg_error:
Debug.Print(Err.Description)
End Sub

The program fails when it finds the first "writer.WriteStartElement" statement (where it contains the ':' carácter).

This is the code I want to generate:

<cfdi:Comprobante
xmlns:cfdi="http://www.sat.gob.mx/cfd/3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.sat.gob.mx/cfd/3
http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd"
……………..
an0ther1 22-Jan-18 19:18pm    
Refer updated solution - you need to use the WriteStartElement(String, String, String) overload method

Kind Regards

1 solution

I did a bit of testing and determined you need to use one of the overloads of XmlWriter.WriteStartElement, specifically; XmlWriter.WriteStartElement Method (String, String, String) (System.Xml)[^]
The below snippet does work & will give you the output that you have requested in your comment;
VB
Dim sett as XmlWriter.Settings = New XMLWriterSettings();
sett.Indent = True
using writer as XmlWriter = XmlWriter.Create("Filename and Path", sett)
writer.WriteStartElement("cfdi", "Comprobante", "http://www.sat.gob.mx/cfd/3")
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-Instance")
writer.WriteAttributeString("xmlns", "schemaLocation", null, "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cdv33.xsd")
' add additional elements
' write end element
writer.WriteEndElement()


New version - open a brand new C# Windows Form project, add a single button to the form. The below code can be entered into the button_click event;

C#
private void button1_Click(object sender, EventArgs e)
        {
            XmlWriter writer = null;

            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                writer = XmlWriter.Create("FileName.xml", settings);

                writer.WriteComment("sample XML fragment");
                writer.WriteStartElement("cfdi", "Comprobante", "http://www.sat.gob.mx/cfd/3");

                writer.WriteAttributeString("xmlns", "xsi", null, @"http://www.w3.org/2001/XMLSchema-Instance");
                writer.WriteAttributeString("xmlns", "schemaLoc", null, @"http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cdv33.xsd");

                writer.WriteStartElement("cfdi", "ElementName", "http://www.sat.gob.mx/cfd/3");
                writer.WriteString("This is my element string");
                writer.WriteEndElement();

                // Write the close tag for the root element.
                writer.WriteEndElement();

                // Write the XML to file and close the writer.
                writer.Flush();
                writer.Close();
            }

            finally
            {
                if (writer != null)
                    writer.Close();
            }
        }


This will output the following XML;

<?xml version="1.0" encoding="utf-8"?>
<!--sample XML fragment-->
<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance" xmlns:schemaLoc="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cdv33.xsd" xmlns:cfdi="http://www.sat.gob.mx/cfd/3">
<cfdi:elementname>This is my element string



Kind Regards
 
Share this answer
 
v5
Comments
Miguel Altamirano Morales 22-Jan-18 19:45pm    
Thanks for your advice, my friend. I just saw the page you suugested me and couldn't find an answer. WriteStartAttribute methid is still telling me the ":" is invalid
Miguel Altamirano Morales 22-Jan-18 20:07pm    
OK. I got what I wanted, but now I have another problem: It only writes the following:

<?xml version='1.0' encoding='utf-8'?>
<cfdi:Comprobante

in the next instruction it gives an error: "El prefijo ""xmlns"" está reservado para su uso en XML." (xmnls prefix is reserved for its use in XML)

Thanks a lot for helping me anOther; you're a nice person and I'd really appreciate your kindness.
an0ther1 22-Jan-18 20:27pm    
No worries, thanks for the translations!!
I go the same error when using writer.WriteAttributeString("xmlns", "xsi", "http://myurl")
You need to use the overload WriteAttributeString(string, string, string, string)
If you use writer.WriteAttributeString("xmlns", "xsi", null, "http://myurl") does it work?

Kind Regards
Miguel Altamirano Morales 23-Jan-18 12:22pm    
I had to go out of the city; as soon as i get back home I'll check this.

Thanks.
Miguel Altamirano Morales 23-Jan-18 18:23pm    
Hi, No, it does not work. The string "xmlns" is the first parameter of the writeattributestring statement, and I read that this parameter is a "prefix" of type long. This is the parameter causing the error.

I think a complete reading or the XMLWriter class is what I need; the sad thing is I don't have time. Another busy weekend !!! (if I don't get fired first).

I hope you can continue helping me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900