Appending prefixes to Elements in XML file

Hi,

I was hoping to get some help regarding setting prefixes for elements of an xml file. I'm trying to do something like this.

<root>

<Prefix1:Body>

<Prefix1:Element1>data1</Prefix1:Element1>

<Prefix1:Element2>data2</Prefix1:Element2>

</Prefix1:Body>

<Prefix2:Element3>data3</Prefix2:Element3>

</root>

where I would like the freedom to assign Prefix1 and Prefix2 to different strings. At the moment I'm trying generate the xml file using VB.NET particularly XmlTextWriter. I using the statement

xml.WriteElementString("Prefix1", "Element1", "Prefix1") defining the prefix and the namespace. However this would add the xmlns:ns = to Element1.

I was wondering if there a simple way to have a prefix associated with an element without going into namespace schemas.

Another option for me is to write the xml document in a text editor in notepad and then populate the data fields by using xmlreader and xmlwriter. Either way, I would like the prefixes to be flexible for the elements in the xml.

I'm not sure which way is simpler but your help or suggestions is greatly appreciated.

Thank you

jche



Answer this question

Appending prefixes to Elements in XML file

  • tiffeyneohelp

    Thanks alot. Your comments were really helpful. I believe that I will ultimately need to use namespaces because my xml document will be parsed by an external source that I would have no control over. Meaning I won't have a say on whether the xml document will have namespaces off or on. Do you know of any good websites that describe and guide a user to use namespace in creating an XML document using VB.NET

    Thanks again.

    jche


  • AndrewBadera

    Namespaces in XML documents are not tied to schema use, rather you use namespaces to characterize your elements or attributes further, to allow them to be distinguished from elements or attributes authored by someone else who might choose the same element or attribute name.

    So the proper way to use prefixes is in the terms of the "Namespaces in XML 1.0" specification. As you can see, that specification has nothing to do with schemas.

    If you really want to have colons in your names but don't want to use namespaces then you have to put your XmlTextWriter in the proper mode:

    XmlTextWriter writer = new XmlTextWriter(Console.Out);

    writer.Namespaces = false;

    writer.WriteStartElement("root");

    writer.WriteElementString("prefix1:element1", "Kibo");

    writer.WriteElementString("prefix2:element2", "Xibo");

    writer.WriteEndElement();

    writer.Close();

    but you should not that in this case the name of element is simply "prefix1:element1" where one of the characters in the name is a colon ':'.

    Consequently if you parse such markup you need to tell the XmlTextReader too to operate in non namespace mode and you will find that prefix is not set e.g.

    XmlTextReader reader = new XmlTextReader(new StringReader(@"<root>

    <prefix1:element1>Kibo</prefix1:element1>

    <prefix2:element2>Xibo</prefix2:element2>

    </root>"));

    reader.Namespaces = false;

    while (reader.Read())

    {

    if (reader.NodeType == XmlNodeType.Element)

    {

    Console.WriteLine("Found element with name: '{0}', prefix: '{1}', local name: '{2}'.", reader.Name, reader.Prefix, reader.LocalName);

    }

    }

    reader.Close();

    the output is

    Found element with name: 'root', prefix: '', local name: 'root'.
    Found element with name: 'prefix1:element1', prefix: '', local name: 'prefix1:element1'.
    Found element with name: 'prefix2:element2', prefix: '', local name: 'prefix2:element2'.

    Frankly if you are currently designing and creating your XML then I would strongly suggest to properly use prefixes and namespaces as the specification mandates it, while XmlTextReader/Writer allow you to switch to a non namespace mode the world of XML and XML parsers and tools is geared towards XML with namespaces and trying to avoid namespaces impairs the the compatibility of your XML.



  • Appending prefixes to Elements in XML file