C# 序言异常中不允许的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/649209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:44:44  来源:igfitidea点击:

content not allowed in prolog exception

c#javasoapcxf

提问by

I am trying to send xml to a java-based web service given to me by a third party via a c#.NET application, and I get the org.xml.sax.SAXParseException: Content is not allowed in prologerror.

我正在尝试将 xml 发送到第三方通过 ac#.NET 应用程序提供给我的基于 Java 的 Web 服务,但org.xml.sax.SAXParseException: Content is not allowed in prolog出现错误。

I have verified the xml against the schema, and I passed the memorystream I am using to hold the xml to an .xml file, then opened the file with a hex editor to make sure that there were no undesired characters in the prolog, and there are none. When opened, the first characters in the file are

我已经根据架构验证了 xml,并将我用来保存 xml 的内存流传递给 .xml 文件,然后用十六进制编辑器打开文件以确保 prolog 中没有不需要的字符,然后都没有。打开时,文件中的第一个字符是

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

The class I was given to use to send the xml data to the web service accepts a byte array. I figure that creating the xml using an XmlTextWriterto a utf-8 encoded memorystream, then sending the contents of the stream to a byte array is the most direct method.

我被用来将 xml 数据发送到 Web 服务的类接受一个字节数组。我认为使用XmlTextWriter创建 xml到 utf-8 编码的内存流,然后将流的内容发送到字节数组是最直接的方法。

I have done a lot of research and tried all the possbilities around this issue that I could find, but nothing works. Could someone please help? Thanks in advance.

我做了很多研究,并尝试了我能找到的围绕这个问题的所有可能性,但没有任何效果。有人可以帮忙吗?提前致谢。

By the way, here is a portion of what the web service returns to me. In the payload of the SOAP message, should the data after the element look like that, or be readable xml like the content before it?

顺便说一下,这是 Web 服务返回给我的部分内容。在 SOAP 消息的有效负载中,应将后面的数据元素看起来像那个,或者像之前的内容一样可读吗?

Messages:
Message:

Payload: <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http:/
/schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema
-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><postSubmissi
on xmlns="http://service.arm.hud.gov/"><submissionHeader><agcHcsId>1</agcHcsId><
agcName>test</agcName><systemName>123</systemName><cmsSubId>123456</cmsSubId><su
bFlag>0</subFlag></submissionHeader><agcType>test</agcType><submissionData>PD94b
WwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48dG5zOlN1Ym1pc3Npb25EYXRhIHhzaTpzY
2hlbWFMb2NhdGlvbj0iaHR0cDovL2dvdi5odWQuYXJtL2FybV9kYXRhYmFnXzNfMS54c2QiIHhtbG5zO
nhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxT......etc............................
</submissionData></postSubmission></soap:Body></soap:Envelope>

Here the xml data for better readability:

这里的 xml 数据为了更好的可读性:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http:/
    /schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema
    -instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <postSubmissi on xmlns="http://service.arm.hud.gov/">
            <submissionHeader>
                <agcHcsId>1</agcHcsId>
                <agcName>test</agcName>
                <systemName>123</systemName>
                <cmsSubId>123456</cmsSubId>
                <subFlag>0</subFlag>
            </submissionHeader>
            <agcType>test</agcType>
            <submissionData>PD94b
                WwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48dG5zOlN1Ym1pc3Npb25EYXRhIHhzaTpzY
                2hlbWFMb2NhdGlvbj0iaHR0cDovL2dvdi5odWQuYXJtL2FybV9kYXRhYmFnXzNfMS54c2QiIHhtbG5zO
                nhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxT......etc............................
            </submissionData>
        </postSubmission>
    </soap:Body>
</soap:Envelope>

回答by Charlie Martin

I just took your message, indented it and it validates:

我刚刚接受了您的消息,将其缩进并进行了验证:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <postSubmission xmlns="http://service.arm.hud.gov/">
            <submissionHeader>
                <agcHcsId>1</agcHcsId>
                <agcName>test</agcName>
                <systemName>123</systemName>
                <cmsSubId>123456</cmsSubId>
                <subFlag>0</subFlag>
            </submissionHeader>
            <agcType>test</agcType>
            <submissionData>
            PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmL *abbreviated data to fit*
            </submissionData>
        </postSubmission>
    </soap:Body>
</soap:Envelope>

I would guess that something just aheadof this message is out of sync somehow -- the error message appears to indicate that the XML parser thinks it's seeing a comment before it sees the <?xmlline.

我猜想这条消息之前的某些东西不知何故不同步——错误消息似乎表明 XML 解析器认为它在看到该<?xml行之前看到了一条注释。

回答by Eddie

Do you have a Byte Order Markat the top of your file that is causing this confusion? Dump or edit the file with a hex dump or hex editor and check the first two or three bytes and ensure that the file starts with

您的文件顶部是否有导致这种混乱的字节顺序标记?使用十六进制转储或十六进制编辑器转储或编辑文件并检查前两三个字节并确保文件以

回答by sourcerebels

Just open your XML file with hex editor like hexplorerand then you can be able to see and delete this strange chars, save your file and open with your preferred editor (personally I use Notepad++) and be sure that your file is using UTF-8 encoding :-)

只需使用十六进制编辑器(如hexplorer)打开您的 XML 文件,然后您就可以查看和删除这些奇怪的字符,保存您的文件并使用您喜欢的编辑器打开(我个人使用Notepad++),并确保您的文件使用的是 UTF-8编码:-)

Hope this will help you

希望能帮到你

回答by Brian Agnew

I've had no end of problems with C#/Java XML interoperability and Java's handling of the byte order mark (2 or 3 bytes preceeding the XML declaration and identifying the encoding byte order). Java doesn't play nicely with a valid BOM, so you'll have to remove it. Check for it by deriving a byte array and using:

我在 C#/Java XML 互操作性和 Java 处理字节顺序标记(XML 声明之前的 2 或 3 个字节并标识编码字节顺序)方面遇到了无穷无尽的问题。Java 不能很好地与有效的 BOM 配合使用,因此您必须将其删除。通过派生一个字节数组并使用来检查它:

arr[0] == (byte) 0xEF && arr[1] == (byte) 0xBB && arr[2] == (byte) 0xBF

This checks for the 3 byte variant, which is what causes grief :-(

这会检查 3 字节变体,这就是导致悲伤的原因:-(

回答by yottamoto

Does the web service operate correctly with other (java) clients? I've received this error quite a few times, and the source of the error was problems with library dependencies - if I remember correctly, something with JAXB2 in connection with Java5.

Web 服务是否与其他 (java) 客户端一起正常运行?我收到过很多次这个错误,错误的根源是库依赖问题——如果我没记错的话,是 JAXB2 与 Java5 相关的问题。

回答by Dana the Sane

I was able to get rid of the problem by removing the UTF encoding. Sounds like in both of our cases, the text wasn't actually UTF-8 encoded.

我能够通过删除 UTF 编码来解决这个问题。听起来在我们的两种情况下,文本实际上都不是 UTF-8 编码的。