1

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 but it does not give me the same layout:

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

I got those errors :

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

The '=' character, hexadecimal value 0x3A, cannot be included in a name.

The ' ' character, hexadecimal value 0x3A, cannot be included in a name.

Thomas
  • 1,445
  • 14
  • 30
  • Review the error messages, they can't all be 0x3A. XML distinguishes between element names and attribute names. "media:credit" is the element, "role" is an attribute of the element. The code snippet passes both to the XElement constructor, but it requires *only* the element name. Check [this post](https://stackoverflow.com/a/5063951/17034) for the code to set attributes. – Hans Passant Jul 19 '18 at 10:36

1 Answers1

1

Here is an example, with just the <media:content.../> element:

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

Please note that you must adapt this line:

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

Replace the string by whatever namespace media has in your XML file. E.g. if your XML file contains something like

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

then change the above line to

Dim ns As XNamespace = "http://other.namespace"
FrankM
  • 1,007
  • 6
  • 15