Results 1 to 4 of 4

Thread: setting an XmlElement its prefix.

  1. #1

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    setting an XmlElement its prefix.

    Hi,

    I would like to add an XmlElement that looks like this:
    <data:lvalue-possibles>

    Here is my current code.
    Code:
    XmlDocument root = new XmlDocument();
    XmlNode attr = root.ImportNode(base.Xml,true);
    root.AppendChild(attr);
    
    XmlElement possibles = root.CreateElement("data:lvalue-possibles");
    possibles.Prefix = "data";
    attr.AppendChild(possibles);
    return attr;//returns the XmlNode
    This code is part of a function which returns an XMLNode. It also overrides a function of another class. It takes the XMLNode returned by that baseclass function and just adds the new tag.

    Here's some explanations of the code above:
    1) To create an XmlElement there is always an XmlDocument needed. So first I create the XmlDocument.
    Code:
    XmlDocument root = new XmlDocument();
    2) As I said earlier the function overrides another function. First we'll need to convert the tags of the basefunction to the newly created document.
    Code:
    XmlNode attr = root.ImportNode(base.Xml,true);
    3) Here we make those tags the root of our document.
    Code:
    root.AppendChild(attr);
    4) Next we create the new XMLElement.
    Code:
    XmlElement possibles = root.CreateElement("data:lvalue-possibles");
    5) I am trying really hard to set the prefix to "data:...".
    Code:
    possibles.Prefix = "data";
    6) After that the new element is added to the previous one.
    Code:
    attr.AppendChild(possibles);
    I have no idea why, but this does not work. Everything works fine that is, except the prefix. No matter what I try the prefix does not show up. The output is this:
    <baseclasstags>
    <lvalue-possibles />
    </baseclasstags>
    When I debug I can see that even right after the 4th step there is no prefix in the InnerXML property. So it looks like it was never added.

    Does anybody know how I can set the prefix anyway?
    Last edited by BramVandenbon; Sep 30th, 2006 at 03:11 PM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  2. #2

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: setting an XmlElement its prefix.

    I read somewhere that I need to add a namespacemanager. Tried it but made no difference:

    Code:
    XmlNamespaceManager n = new XmlNamespaceManager(root.NameTable);
    n.AddNamespace("data", "http://lamp.cfar.umd.edu/viperdata");
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: setting an XmlElement its prefix.

    The prefix is called a namespace. In the W3C DOM you use the createElementNS() method to add an element using a qualified namespace. In the .NET class it is implemented using an overload of CreateElement.

    http://msdn2.microsoft.com/en-us/lib...teelement.aspx

    Code:
    XmlElement possibles = root.CreateElement(
      "lvalue-possibles",
      "data",
      "http://lamp.cfar.umd.edu/viperdata"
    );

  4. #4

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: setting an XmlElement its prefix.

    Thank you seems like it did indeed work like that !
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width