VB NET中的wmlwriter问题 [英] Problem with wmlwriter in VB NET

查看:94
本文介绍了VB NET中的wmlwriter问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我正在开发一个Visual Studio 2017(Visual Basic)项目,尝试生成一个XML文件,由适当的DAC验证,用于电子发票。 />


- 我正在使用过去使用的xmlWriter类,通过Visual Studio 2010,它运行得很好。



但现在,使用我之前使用的相同语句,当我尝试使用'WriteStartElement'方法时,它会失败并发送消息:



Carácterdenombrenováliden'cfdi:Comprobante'.Elcarácter':',con valor hexadecimal 0x3A,no puede incluirse en un nombre。



In英语(或多或少)名称中的字符无效。具有十六进制值0x3A的字符':'不能包含在名称中。



- 我也曾经使用ascii字符(34)连接文本字符串中的''字符,该字符应该包含在文字的一部分中,但它会生成一对''',而不仅仅是我想要的一个。像这样:



Dim Cadena As String =:xsi =+ Chr(34)+http://www.w3.org/2001/XMLSchema-实例+ 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



任何人都可以帮助我或暗示在哪里寻找这对疑惑吗?



提前致谢。



我尝试了什么:



我一直在寻找通过互联网,我还没有找到任何东西。我将继续寻找一些东西,但我会感激任何帮助。



我不知道为什么同样的例程以前工作正常而不是现在。

解决方案

我做了一些测试并确定你需要使用XmlWriter.WriteStartElement的一个重载,特别是; XmlWriter.WriteStartElement方法(String,String,String) (System.Xml) [ ^ ]

以下代码段可以正常使用&将为您提供评论中所要求的输出;

  Dim  sett  as  XmlWriter.Settings =  XMLWriterSettings(); 
sett.Indent = True
使用 writer as XmlWriter = XmlWriter.Create( 文件名和路径,设置)
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
' 添加其他元素
' 写结束元素
writer.WriteEndElement()





新版本 - 打开一个全新的C#Windows窗体项目,在表单中添加一个按钮。以下代码可以输入button_click事件;



  private   void  button1_Click( object  sender,EventArgs e)
{
XmlWriter writer = < span class =code-keyword> null ;

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

writer.WriteComment( 示例XML片段);
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( 这是我的元素字符串);
writer.WriteEndElement();

// 写下根元素的关闭标记。
writer.WriteEndElement();

// 将XML写入文件并关闭编写器。
writer.Flush();
writer.Close();
}

最后
{
如果(writer!= null
writer.Close();
}
}





这将输出以下XML;



<?xml version =1.0encoding =utf-8?>

<! - 示例XML片段 - >

< cfdi:Comprobante xmlns:xsi =http://www.w3.org/2001/XMLSchema-Instancexmlns:schemaLoc =http://www.sat.gob.mx/cfd/3 http ://www.sat.gob.mx/sitio_internet/cfd/3/cdv33.xsdxmlns:cfdi =http://www.sat.gob.mx/cfd/3>

< cfdi:elementname>这是我的元素字符串







亲切的问候


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.

解决方案

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;

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;

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


这篇关于VB NET中的wmlwriter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆