我该如何处理XML中的URL?

时间:2021-12-02 00:19:20

I'm creating an XML document and I want to store URLs inside the a node like so:

我正在创建一个XML文档,我想将URL存储在一个节点中,如下所示:

<ns:url>http://example.com</ns:ulr>

My question is, do I need to encode/escape the URL in anyway?

我的问题是,我还需要编码/转义URL吗?

If I do, the will the .Net XmlDocument object handle this for me?

如果我这样做,.Net XmlDocument对象会为我处理这个吗?

3 个解决方案

#1


In general most DOM implementations (including XmlDocument) should handle any necessary escaping of text content by default.

通常,大多数DOM实现(包括XmlDocument)应该默认处理任何必要的文本内容转义。

#2


The DOM/XmlWriter/whatever you are using should handle that for you. One minor point: you might find it easier to use XDocument (if you have 3.5) - the namespace usage is much simpler (IMO):

DOM / XmlWriter /您正在使用的任何内容都应该为您处理。一个小问题:您可能会发现使用XDocument更容易(如果您有3.5) - 名称空间使用更简单(IMO):

XNamespace ns = "http://consoso/foobar";
XDocument doc = new XDocument(
     new XElement("Foo",
         new XAttribute(XNamespace.Xmlns + "ns", ns), // alias
         new XElement("Bar", "abc"),
         new XElement(ns + "url", "http://foo/bar")
     )
 );
 string s = doc.ToString();

Which creates:

<Foo xmlns:ns="http://consoso/foobar">
  <Bar>abc</Bar>
  <ns:url>http://foo/bar</ns:url>
</Foo>

#3


I doubt there will be any need to escape it in general. However, you might have some application level requirement to do so.

我怀疑一般都不需要逃避它。但是,您可能需要一些应用程序级别的要求。

You should absolutely encode the URL as XML text (for example, it shouldn't contain >).

您应该将URL绝对编码为XML文本(例如,它不应包含>)。

Clarification: This does not mean you should pass the encoded text to the DOM implementation. All XML APIs I know do that for you; so for all practical situations, you wouldn't need to do it manually. I just said it should be done.

澄清:这并不意味着您应该将编码文本传递给DOM实现。我知道的所有XML API都是为您做的;因此,对于所有实际情况,您不需要手动执行此操作。我只是说它应该完成。

#1


In general most DOM implementations (including XmlDocument) should handle any necessary escaping of text content by default.

通常,大多数DOM实现(包括XmlDocument)应该默认处理任何必要的文本内容转义。

#2


The DOM/XmlWriter/whatever you are using should handle that for you. One minor point: you might find it easier to use XDocument (if you have 3.5) - the namespace usage is much simpler (IMO):

DOM / XmlWriter /您正在使用的任何内容都应该为您处理。一个小问题:您可能会发现使用XDocument更容易(如果您有3.5) - 名称空间使用更简单(IMO):

XNamespace ns = "http://consoso/foobar";
XDocument doc = new XDocument(
     new XElement("Foo",
         new XAttribute(XNamespace.Xmlns + "ns", ns), // alias
         new XElement("Bar", "abc"),
         new XElement(ns + "url", "http://foo/bar")
     )
 );
 string s = doc.ToString();

Which creates:

<Foo xmlns:ns="http://consoso/foobar">
  <Bar>abc</Bar>
  <ns:url>http://foo/bar</ns:url>
</Foo>

#3


I doubt there will be any need to escape it in general. However, you might have some application level requirement to do so.

我怀疑一般都不需要逃避它。但是,您可能需要一些应用程序级别的要求。

You should absolutely encode the URL as XML text (for example, it shouldn't contain >).

您应该将URL绝对编码为XML文本(例如,它不应包含>)。

Clarification: This does not mean you should pass the encoded text to the DOM implementation. All XML APIs I know do that for you; so for all practical situations, you wouldn't need to do it manually. I just said it should be done.

澄清:这并不意味着您应该将编码文本传递给DOM实现。我知道的所有XML API都是为您做的;因此,对于所有实际情况,您不需要手动执行此操作。我只是说它应该完成。