【问题标题】:How to encode/decode XML in VBA and C#如何在 VBA 和 C# 中编码/解码 XML
【发布时间】:2012-07-04 06:01:39
【问题描述】:

我目前有一个程序可以将数据从 VBA(Access)传输到 C#(通过 ASP.NET POST)。如何在每一端进行编码/解码?

这是我用 C# 读取的 XML:

StreamReader reader = new StreamReader(Request.InputStream);
string xmlData = "";
XmlDocument xml = new XmlDocument();
xmlData = reader.ReadToEnd();
XmlElement rootXML;
xml.LoadXml(xmlData);
rootXML = xml.DocumentElement;

这是我用 C# 编写的 XML:

Response.Clear();
Response.ContentType = "text/xml";
Response.Charset = "UTF-8";
Response.Write(XML_in_String);
Response.End();

这是我用 VBA 编写的 XML

connection.Open "POST", server & "www.webpage.com/" & postData, False
connection.setRequestHeader "Accept", "application/xml"
connection.setRequestHeader "Content-Type", "application/xml"
connection.send "<?xml version=""1.0"" encoding=""UTF-8"" ?><data>" & outXMLstr & "</data>"

这是我在 VBA 中读取的 XML

Dim inXML As MSXML2.DOMDocument
Set inXML = New DOMDocument
inXML.loadXML (connection.responseText)

具体来说,我在尝试加载 XML(xmlData) 时在 C# 中遇到错误,因为它无法解析与号 (&)。

【问题讨论】:

  • 你想将 & 编码为 &使用 Server.HtmlEncode & Server.HtmlDecode
  • 在你的 XML 中有问题的 & 符号在哪里?你能展示 XML 的那部分吗?

标签: c# asp.net ms-access vba


【解决方案1】:

在您的xmlData 上,您是否按照@HatSoft 所说的那样尝试使用Server.HtmlEncode/Server.HtmlDecode?这样您就可以摆脱在 XML 中不起作用的 htmlentities。

或者您可以将数据包装到 CDATA 字段中

Decode CDATA section in C#

http://www.w3schools.com/xml/xml_cdata.asp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多