XML editing

When i try to use any of the XML editing functions within the XPath class i always get a method is not supported error.  Why is this



' Load the document.
Dim Doc As New XPathDocument("Data.xml"
)
' Navigate the document with an XPathNavigator.
Dim Navigator As
XPathNavigator = Doc.CreateNavigator()
' Move to the root <Debts> element.
Navigator.MoveToRoot()
' Move to the first contained <User > element.
Navigator.MoveToFirstChild()
' Write a new Child
Dim Writer As
XmlWriter = Navigator.AppendChild() ' ERROR HERE!!!!!!
' And insert the tag
navigator.AppendChild.WriteStartElement("User"
+ NoOfDebtRecs.ToString())
' And the data
Writer.WriteElementString("Name"
, TextReturned.ToString())
' Clear the text box for next time
AddUser.TextBoxUserName.Text =
""

 


Answer this question

XML editing

  • embedded

    XPathDocument is optimized for fast, read-only access to your XML document. Try changing to XmlDocument, which allows read/write access, and you should be fine.

  • DevJohan

    Its so much more easy when you know how!

    Thanks James.

    Phil WInder


  • Julia Semenova

    Let me try to explain. The underlying document object model (DOM) can be XPathDocument or XmlDocument (or a few others like XmlDataDocument). Each is optimized for a different scenario. XPathDocument is like a read-only, forward-only cursor in a database. You can call update methods using the cursor, but they will fail because the cursor isn't writable. You can also open an updatable cursor like a keyset cursor. You use the same methods and you will successfully update the data. Does this make more sense



  • Nidal-Fouad-Hajj-Youssef

    Well i dont really understand that but ill take your word for it.  Thanks.Idea
  • HS-Immeronline

    ok so how do i use something like a keyset cursor because im having problems adding elements in a specific place.  Thanks again,

    Phil Winder

  • MarkR&amp;#42;&amp;#42;&amp;#42;

    Thanks James Big Smile

    Phil Winder


  • Mathieu Cupryk

    It's as simple as using XmlDocument where you had XPathDocument.

    ' Load the document.
    Dim Doc As New XmlDocument("Data.xml"
    )
    ' Navigate the document with an XPathNavigator.
    Dim Navigator As
    XPathNavigator = Doc.CreateNavigator()
    ' Move to the root <Debts> element.
    Navigator.MoveToRoot()
    ' Move to the first contained <User > element.
    Navigator.MoveToFirstChild()
    ' Write a new Child
    Dim Writer As
    XmlWriter = Navigator.AppendChild() ' ERROR HERE!!!!!!
    ' And insert the tag
    navigator.AppendChild.WriteStartElement("User"
    + NoOfDebtRecs.ToString())
    ' And the data
    Writer.WriteElementString("Name"
    , TextReturned.ToString())
    ' Clear the text box for next time
    AddUser.TextBoxUserName.Text =
    ""

  • JosAnt

    I dont think your quite right since

    "The < XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" />XPathNavigator class provides a set of methods used to insert sibling, child and attribute nodes in an XML document. In order to use these methods, the XPathNavigator object must be editable, that is, its CanEdit property must be true."



    Thanks

    Phil Winder

  • mshola

    XPathNavigator does provide insertion provided that you run it against a DOM that is read/writable, which XPathDocument is not.

  • XML editing