0

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.

Daniel
  • 374
  • 1
  • 12

1 Answers1

2

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>
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
  • Thank you, Yitzhak, for your answer and your approach to solving this problem. I have shown this to my colleague. We were able to create a reasonable XML node as we need it. Thank you! There is a next problem now, but it has nothing to do with node building per se and I need to communicate that in the meeting tomorrow. Sorry for the late reply. – Daniel Oct 09 '22 at 16:53