locked
The ':' character, hexadecimal value 0x3A, cannot be included in a nameThe ':' character, hexadecimal value 0x3A, cannot be included in a name RRS feed

  • Question

  • I'm trying to write a code to insert items in exist xml file ,I want the items in file to be like this:

    <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>

    so I have write this code :

           

    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",
                                          New XAttribute("role", "author"),
                                          New XAttribute("><![CDATA[", TextBox5.Text + "]]>"))

            root.Add(title, link, pubDate, description, author)
            document.Root.Add(root)
            document.Save(FilePath)
            Label1.Text = "done"

        End Sub

    I got this error :The ':' character, hexadecimal value 0x3A, cannot be included in a name.

    Thursday, July 19, 2018 4:39 PM

All replies

  • You can't name an element <media:cateogry> etc.  The error is telling you that is not a valid character in an XML element name.

    Why not simply create a <media> element and then put <category>, <content>, and <thumbnail> as child elements?

    Another option would be to replace the colon with a valid character such as underscore.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, July 19, 2018 5:16 PM
  • Oh sorry, those names already exist in the file.  There must a namespace in use that isn't shown.  You'll need to add the namespace to the document and set it on the element through an attribute.

    -EDIT-

    here is some guidance... though the examples are in C# it should be easy enough to understand the equivalent in VB:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-create-a-document-with-namespaces-linq-to-xml


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Thursday, July 19, 2018 5:19 PM
  • Check the next method too:

    Dim d As New XmlDocument

    d.Load("XMLFile1.xml")

    Using w As XmlWriter = d.CreateNavigator().SelectSingleNode("/*").AppendChild()

       w.WriteStartElement("item")

       w.WriteElementString("title", "some title")

       w.WriteStartElement("credit", "some namespace")

       w.WriteAttributeString("role", "author")

       w.WriteString("some author")

       w.WriteEndElement()

       ' . . .

       w.WriteEndElement()

    End Using

    d.Save("XMLFile1.xml")

     

     

    It assumes that the root node of the existing (loaded) document already contains the corresponding definition of "media" namespace, such as:

    <?xml version="1.0" encoding="utf-8"?>

    <items xmlns:media="some namespace">
       . . .

    </items>

     

    See the XmlWriter class for details.




    Thursday, July 19, 2018 7:00 PM