Home > Software engineering >  Missing word in XML, although included in function?
Missing word in XML, although included in function?

Time:10-10

By writing this

Dim AiD As XmlElement = myXML.CreateElement("ns8:AiD", aNamespace)
AiD.InnerText = "myInnerText"
AiD.SetAttribute("xsi:type", "ns17:anObject")
AiD.SetAttribute("xmlns:ns17", "http://aLink")
AiD.SetAttribute("xmlns:xsi", "http://anotherLink")
WR.AppendChild(AiD)

I want my XML file to contain this:

<ns8:AiD xsi:type="ns17:anObject" xmlns:ns17=http://aLink xmlns:xsi=http://anotherLink>myInnerText</ns8:AiD>

Unfortunately, it contains this:

<ns8:AiD type="ns17:anObject" xmlns:ns17="http://aLink" xmlns:xsi="http://anotherLink">myInnerText</ns8:AiD>

So quotation marks are included and ‘xsi:’ is missing. I have already read that quotation marks are not bad. But why is ‘xsi:’ missing ?

The functions are the usual .NET System.Xml functions. MyXML is a System.XML.XmlDocument.

CodePudding user response:

Just in case, here is c# implementation of what you need.

Please check the correct XML output. All namespaces are explicitly declared in the XML itself, otherwise they cannot be used.

Here is VB.NET way of doing it: How to create a document with namespaces in Visual Basic (LINQ to XML)

c#

void Main()
{
    XNamespace ns8 = "http://www.missingNamespace.com";
    XNamespace xsi = "http://anotherLink";
    XNamespace ns17 = "http://aLink";

    XElement AiD = new XElement(ns8   "AiD",
        new XAttribute(XNamespace.Xmlns   "ns8", ns8.NamespaceName),
        new XAttribute(XNamespace.Xmlns   "ns17", ns17.NamespaceName),
        new XAttribute(XNamespace.Xmlns   "xsi", xsi.NamespaceName),
        new XAttribute(xsi   "type", "ns17:anObject"),
        "myInnerText");
    Console.WriteLine(AiD );
}

Output

<ns8:AiD xmlns:ns8="http://www.missingNamespace.com" xmlns:ns17="http://aLink" xmlns:xsi="http://anotherLink" xsi:type="ns17:anObject">myInnerText</ns8:AiD>
  • Related