在 XML 文档中存储等号 (=)

发布于 2024-09-18 07:26:37 字数 502 浏览 3 评论 0原文

我遇到了 Google 还无法解决的问题!

我正在尝试将 URL 存储在 XML 文件中。问题是这些 URL 中包含等号 (=)。这会产生一个错误。

这是我的代码:(**token是一个包含URL的变量)

Dim child As String = vbCrLf & "<Link URL='" & token & "'></Link>"
Dim fragment As XmlDocumentFragment = doc.CreateDocumentFragment
fragment.InnerXml = child

错误消息:(错误行和位置在这里没有意义)

'=' 是一个意外的标记。预期的标记是“;”。第 2 行,位置 133。

我已经替换了所有 '&'带有“&amp”的符号,以防它们是导致错误的原因,但到目前为止还没有运气。

I'm facing a problem that Google couldn't solve yet!

I'm trying to store URLs in an XML file. Problem is that these URLs contain Equal Signs (=) in them. And that generates an error.

Here is my code: (**token is a variable that contains the URL)

Dim child As String = vbCrLf & "<Link URL='" & token & "'></Link>"
Dim fragment As XmlDocumentFragment = doc.CreateDocumentFragment
fragment.InnerXml = child

The error message: (Error line and position are meaningless here)

'=' is an unexpected token. The expected token is ';'. Line 2, position 133.

I've replaced all '&' symbols with '&' in case they were the ones causing the error, but no luck so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤凫 2024-09-25 07:26:37

切勿使用字符串操作来创建 XML。如果您使用 .NET 的 XML API,它们将为您处理所有特殊字符。尝试:

XmlElement linkElement = doc.CreateElement("Link");
XmlAttribute urlAttribute = doc.CreateAttribute("URL");
urlAttribute.Value = token;
linkElement.SetAttributeNode(urlAttribute);
fragment.AppendChild(linkElement);

You should never use string manipulation to create XML. If you use the XML APIs of .NET, they will take care of all the special characters for you. Try:

XmlElement linkElement = doc.CreateElement("Link");
XmlAttribute urlAttribute = doc.CreateAttribute("URL");
urlAttribute.Value = token;
linkElement.SetAttributeNode(urlAttribute);
fragment.AppendChild(linkElement);
画▽骨i 2024-09-25 07:26:37

您不应将 & 替换为 &,而应将其替换为 &

或者更好的是,在片段中创建一个节点并向其添加一个属性。这样该对象将为您正确编码数据。

Dim fragment As XmlDocumentFragment = doc.CreateDocumentFragment()
Dim node As XmlElement = doc.CreateElement("Link")
Dim attr as XmlAttribute = doc.CreateAttribute("URL")
attr.Value = token
node.Attributes.Append(attr)
fragment.AppendNode(node)

You should not replace & with &, you should replace them with &.

Or better yet, create a node in your fragment and add an attribute to it. That way the object will encode the data correctly for you.

Dim fragment As XmlDocumentFragment = doc.CreateDocumentFragment()
Dim node As XmlElement = doc.CreateElement("Link")
Dim attr as XmlAttribute = doc.CreateAttribute("URL")
attr.Value = token
node.Attributes.Append(attr)
fragment.AppendNode(node)
浮萍、无处依 2024-09-25 07:26:37

尝试在第一行下面添加这一行:

-- insert this line; should make the "=" sign safe for XML...
token = System.Web.HttpUtility.UrlEncode(token)

Dim child As String = vbCrLf & "<Link URL='" & token & "'></Link>"
Dim fragment As XmlDocumentFragment = doc.CreateDocumentFragment
fragment.InnerXml = child

Try adding this line below the first line:

-- insert this line; should make the "=" sign safe for XML...
token = System.Web.HttpUtility.UrlEncode(token)

Dim child As String = vbCrLf & "<Link URL='" & token & "'></Link>"
Dim fragment As XmlDocumentFragment = doc.CreateDocumentFragment
fragment.InnerXml = child
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文