【问题标题】:Hexadecimal values cannot be included in a name vb.net十六进制值不能包含在名称 vb.net 中
【发布时间】:2018-07-19 08:51:17
【问题描述】:

我正在尝试编写代码以在现有 xml 文件中插入项目,我希望文件中的项目如下所示:

<item>
  <title><![CDATA[any title]]></title>
  <link>http://any link</link>
  <pubDate>any date</pubDate>
  <guid isPermaLink="true">any link</guid>
  <description><![CDATA[any description]]></description>
        <media:credit role="author"><![CDATA[any author]]></media:credit>
  <media:category><![CDATA[any category]]></media:category>
  <media:content url="http://any link" height="266" width="127" /> 
  <media:thumbnail url="http://any link" height="266" width="127" />
</item>

所以我写了这段代码,但它没有给我相同的布局:

Dim FilePath As String
    FilePath = "C:\Users\MONZER\Desktop\Karary Web Site\WebApplication1\XMLFile1.xml"
    Dim document As New XDocument
    If File.Exists(FilePath) Then
        document = XDocument.Load(FilePath)
    Else
        Label1.Text = "not done"
    End If

    Dim root = New XElement( "item")
    Dim title = New XElement("title", "<![CDATA[" & TextBox3.Text & "]]>")
    Dim link = New XElement("link", TextBox6.Text)
    Dim pubDate = New XElement("pubDate", DateTime.Now.ToString("yyy/MM/dd HH:mm"))
    Dim description = New XElement("description", TextBox5.Text)
    Dim author = New XElement("media:credit role=", "author" & "><![CDATA[" & TextBox5.Text & "]]>")
    Dim category = New XElement("media:category", "<![CDATA[" & TextBox7.Text & "]]>")
    Dim content = New XElement("media:content url=", "http://anylink" & " height=" & "266" & " width=" & "127")
    Dim thumbnail = New XElement("media:thumbnail url=", "http://anylink" & " height=" & "266" & " width=" & "127")
    root.Add(title, link, pubDate, description, author, category, content, thumbnail)
    document.Root.Add(root)
    document.Save(FilePath)
    Label1.Text = "done"

End Sub

我收到了这些错误:

':' 字符,十六进制值 0x3A,不能包含在 名字。

'=' 字符,十六进制值 0x3A,不能包含在 名字。

' ' 字符,十六进制值 0x3A,不能包含在 名字。

【问题讨论】:

  • 查看错误信息,它们不可能都是0x3A。 XML 区分元素名称和属性名称。 “media:credit”是元素,“role”是元素的属性。代码 sn-p 将两者都传递给 XElement 构造函数,但它只需要元素名称。检查this post 设置属性的代码。

标签: xml vb.net


【解决方案1】:

这是一个示例,只有 &lt;media:content.../&gt; 元素:

Dim FilePath As String
FilePath = "C:\Temp\myfile.xml"
Dim document As New XDocument
If File.Exists(FilePath) Then
    document = XDocument.Load(FilePath)
Else
    Console.WriteLine("File is missing")
    Exit Sub
End If

' Example: Write
' <item>
'    <media:content url="http://any link" height="266" width="127" /> 
' </item>

Dim ns As XNamespace = "http://that.is.my.namespace"
Dim newItem = New XElement( "item",
                         New XElement(ns + "content",
                                      New XAttribute("url", "http://any link"),
                                      New XAttribute("height", 266),
                                      New XAttribute("width", 127)))

document.Root.Add(newItem)
document.Save(FilePath)

Console.WriteLine("Done")

请注意,您必须修改此行:

Dim ns As XNamespace = "http://that.is.my.namespace"

用您的 XML 文件中的任何命名空间 media 替换字符串。 例如。如果您的 XML 文件包含类似

<items xmlns:media="http://other.namespace">

然后将上面的行改为

Dim ns As XNamespace = "http://other.namespace"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多