VB.NET Setting an attribute

Last post 07-05-2005 4:00 PM by tomasr. 4 replies.

Sort Posts:

  • VB.NET Setting an attribute

    07-05-2005, 12:49 PM
    Hello everyone. I have an XML file and I need to add an attribute to an element at runtime. I have been trying to do it for a long time but its not working. Someone please help. This is part of my XML

    <?xml version="1.0" encoding="utf-8"?>
    <manifest identifier="SingleCourseManifest644" version="1.0" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1">
     <metadata xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_v1p3">
       <schema>ADL SCORM</schema>
       <schemaversion>CAM 1.3</schemaversion>
     </metadata>
     <organizations>
       <organization identifier="Course644" structure="hierarchical">
         <title>Introduction to KMx</title>
         <item identifier="Course_Part213" isvisible="true">
           <title>Compaq iPAQ Pocket PC</title>
    .........

    In the organizations element(bold) I need to add an attribute called default and it should have the value Course644 which is the value of the attribute identifier in the organization element. I am using this code to get the value of the identifier attribute

    Dim value As String = String.Empty

    Dim reader As New XmlNodeReader(NewSCORMManifest)

    While reader.Read

    Select Case reader.NodeType

    Case XmlNodeType.Element

    If reader.Name = "organization" Then

    value = reader.GetAttribute("identifier")

    End If

    End Select

    End While

    This code is working but I cannot add the attribute. I tried using setAttribute() but it gives me an error. This is the code I was trying

    Dim root As XmlElement = NewSCORMManifest.DocumentElement.SelectSingleNode("/organizations")
    root.SetAttribute("default", value)

    And I get the error System.NullReferenceException: Object reference not set to an instance of an object. on this line root.SetAttribute("default", value)

    Please help. Thank you.

    Dog and wife missing. Reward for Dog.
  • Re: VB.NET Setting an attribute

    07-05-2005, 2:41 PM
    • Contributor
      4,280 point Contributor
    • tomasr
    • Member since 04-28-2003, 7:18 AM
    • Colombia
    • Posts 852
    You can't modify your document through a reader. A better option here would be to do an xpath query over the XmlDocument instance to find the element, and then create the new attribute there. Pseudocode:

    XmlNamespaceManager xmlns = new XmlNamespaceManager(doc.NameTable);
    xmlns.AddNamespace("x", "http://www.imsglobal.org/xsd/imscp_v1p1");
    XmlNode orgID = doc.SelectSingleNode("//x:organization/@identifier", xmlns);
    if ( org != null ) {
        XmlNode orgs = doc.SelectSingleNode("//x:organizations", xmlns);
        XmlAttribute attr = doc.CreateAttribute("default");
        attr.Value = orgID.Value;
        orgs.Attributes.Append(attr);
    }
    The xpath expressions here are not the most efficient, but they can certainly be improved fairly easily.

    Tomas Restrepo [MVP]
    tomasr@mvps.org
  • Re: VB.NET Setting an attribute

    07-05-2005, 2:46 PM
    Hi Tomasr. Thanks for the reply. I'll try implementing that and let you know. Thanks again.
    Dog and wife missing. Reward for Dog.
  • Re: VB.NET Setting an attribute

    07-05-2005, 3:00 PM
    Worked perfectly. If you have time can you explain the code to me. Only if you have time. Thank you so much.
    Dog and wife missing. Reward for Dog.
  • Re: VB.NET Setting an attribute

    07-05-2005, 4:00 PM
    • Contributor
      4,280 point Contributor
    • tomasr
    • Member since 04-28-2003, 7:18 AM
    • Colombia
    • Posts 852
    Sure no problem. The code is actually quite simple, taking this into account:
    1- I create an XmlNamespaceManager instance, which is used to associate prefixes (x) to XML namespaces. This allows you to easily use an XPath query later on that takes into account the fact that the elements in the XML document are associated with a particular namespace
    2- I do an XPath query that returns the Identifier attribute of the <organization> element. Notice how the XmlNamespaceManager instance is passed as an argument to SelectSingleNode() so that it understands what XML namespace the x prefix is bound to.
    3- If the attribute was not null (i.e. it exists), the code again does another XPath query to get the <organizations> node (the parent). Then it creates an attribute named default with the same value as the attribute found on step 2, and adds it to the <organizations> element.

    That's it.

    Tomas Restrepo [MVP]
    tomasr@mvps.org
Page 1 of 1 (5 items)