我的任务是将restful web 服务的结果转换为具有新格式的XML 文档。
要转换的 html/xhtml 示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>OvidWS Result Set Resource</title>
</head>
<body>
<table id="results">
<tr>
<td class="_index">
<a class="uri" href="REDACTED">1</a>
</td>
<td class="au">
<span>GILLESPIE JB</span>
<span>KUKES RE</span>
</td>
<td class="so">A.M.A. American Journal of Diseases of Children</td>
<td class="ti">Acetylsalicylic acid poisoning with recovery.</td>
<td class="ui">20267726</td>
<td class="yr">1947</td>
</tr>
<tr>
<td class="_index">
<a class="uri" href="REDACTED">2</a>
</td>
<td class="au">BASS MH</td>
<td class="so">Journal of the Mount Sinai Hospital, New York</td>
<td class="ti">Aspirin poisoning in infants.</td>
<td class="ui">20265054</td>
<td class="yr">1947</td>
</tr>
</table>
</body>
</html>
理想情况下,我要做的就是将列出的任何内容作为类属性并将其设为元素名称,在没有“类”属性的情况下,我只想将其标记为项目。
这是我正在寻找的转换:
<results>
<citation>
<_index>
<uri href="REDACTED">1</uri>
</_index>
<au>
<item>GILLESPIE JB</item>
<item>KUKES RE</item>
</au>
<so>A.M.A. American Journal of Diseases of Children</so>
<ti>Acetylsalicylic acid poisoning with recovery.</ti>
<ui>20267726</ui>
<yr>1947</yr>
</citation>
<citation>
<_index>
<uri href="REDACTED">2</a>
</_index>
<au>BASS MH</au>
<so>Journal of the Mount Sinai Hospital, New York</so>
<ti>Aspirin poisoning in infants.</ti>
<ui>20265054</ui>
<yr>1947</yr>
</citation>
</results>
我在这里找到了一小段代码,它允许我重命名一个节点:
Public Shared Function RenameNode(ByVal e As XmlNode, newName As String) As XmlNode
Dim doc As XmlDocument = e.OwnerDocument
Dim newNode As XmlNode = doc.CreateNode(e.NodeType, newName, Nothing)
While (e.HasChildNodes)
newNode.AppendChild(e.FirstChild)
End While
Dim ac As XmlAttributeCollection = e.Attributes
While (ac.Count > 0)
newNode.Attributes.Append(ac(0))
End While
Dim parent As XmlNode = e.ParentNode
parent.ReplaceChild(newNode, e)
Return newNode
End Function
但是在对 XmlAttributeCollection 进行迭代时会出现问题。由于某种原因,在查看其中一个 td 节点时,源中没有出现的 2 个属性神奇地出现了:rowspan 和 colspan。似乎这些属性与迭代器混淆,因为当它们被消耗时,它们不会像“类”属性那样从属性列表中消失。而是消耗属性的值(从“1”变为“”)。这会导致无限循环。
我注意到它们属于“XMLUnspecifiedAttribute”类型,但是当我修改循环以检测到:
While (ac.Count > 0) And Not TypeOf (ac(0)) Is System.Xml.XmlUnspecifiedAttribute
newNode.Attributes.Append(ac(0))
End While
我收到以下错误:
System.Xml.XmlUnspecifiedAttribute is not accessible in this context because it is 'friend'
任何想法为什么会发生这种情况或如何解决它?