{"id":833,"date":"2010-06-09T16:27:00","date_gmt":"2010-06-09T16:27:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2010\/06\/09\/removing-duplicate-namespaces-in-xml-literals-shyam-namboodiripad\/"},"modified":"2024-07-05T12:41:05","modified_gmt":"2024-07-05T19:41:05","slug":"removing-duplicate-namespaces-in-xml-literals-shyam-namboodiripad","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/removing-duplicate-namespaces-in-xml-literals-shyam-namboodiripad\/","title":{"rendered":"Removing duplicate namespaces in XML Literals (Shyam Namboodiripad)"},"content":{"rendered":"<p><span><span style=\"font-size: small\">A common problem that one often runs into with XML literals and the LINQ to XML API is duplicate XML namespaces. Consider the following example. The code <\/span><a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb384589.aspx\" rel=\"noopener\"><span style=\"font-size: small\">imports a default XML namespace<\/span><\/a><span style=\"font-size: small\"> &#8211; &ldquo;hello&rdquo;.<\/span><\/span><\/p>\n<p><span><span style=\"font-size: small\"><span style=\"text-decoration: underline\">Code:<\/span> <\/span><br \/><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\"><br \/>Imports<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> &lt;<span style=\"color: #b96464\">xmlns<\/span><span style=\"color: #6464b9\">=<\/span><span style=\"color: #555555\">&#8220;<\/span><span style=\"color: #6464b9\">hello<\/span><span style=\"color: #555555\">&#8220;<\/span>&gt; <\/p>\n<p><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\">Module<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: #2b91af\">Module1 <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">Sub<\/span> Main() <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">Dim<\/span> x = <span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">A<\/span><span style=\"color: #6464b9\">&gt; <br \/><\/span><\/span><span style=\"font-family: consolas;color: #555555;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"background: yellow\">&lt;%=<\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;<\/span> <span style=\"background: yellow;color: #555555\">%&gt; <br \/><\/span><\/span><span style=\"font-family: consolas;color: #555555;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"font-family: consolas;color: #6464b9;font-size: 9.5pt\">&lt;\/<\/span><span style=\"font-family: consolas;color: #844646;font-size: 9.5pt\">A<\/span><span style=\"font-family: consolas;color: #6464b9;font-size: 9.5pt\">&gt; <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af\">Console<\/span>.WriteLine(<span style=\"color: #a31515\">&#8220;x:&#8221;<\/span>) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af\">Console<\/span>.WriteLine(x) <\/p>\n<p><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">Dim<\/span> y = <span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">A<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">A<\/span><span style=\"color: #6464b9\">&gt; <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y.Add(<span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;<\/span>) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af\">Console<\/span>.WriteLine(<span style=\"color: #a31515\">&#8220;y:&#8221;<\/span>) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af\">Console<\/span>.WriteLine(y) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">End<\/span> <span style=\"color: blue\">Sub <br \/><\/span><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\">End<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: blue\">Module <\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"text-decoration: underline\">Output<\/span>: <br \/><\/span><span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d;font-size: 11pt\"><span><br \/><span style=\"font-size: small\">x: <br \/><\/span><\/span><span style=\"font-size: small\"><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d\"><span style=\"font-family: Segoe UI;color: #000000\">&lt;A xmlns=&#8221;hello&#8221;&gt; <br \/>&nbsp; &lt;B&gt;&lt;\/B&gt; <br \/><\/span><\/span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d\"><span style=\"font-family: Segoe UI;color: #000000\">&lt;\/A&gt;<\/span><\/span><\/span><\/span><span style=\"font-size: small\"> <\/p>\n<p><\/span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d;font-size: 11pt\"><span style=\"font-family: Segoe UI;color: #000000\"><span style=\"font-size: small\">y: <br \/><\/span><\/span><\/span><span style=\"font-size: small\"><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d\"><span style=\"font-family: Segoe UI;color: #000000\">&lt;A <strong><span style=\"color: #ff0000\">xmlns=&#8221;hello&#8221;<\/span><\/strong>&gt; <br \/><strong>&nbsp; <\/strong>&lt;B <span style=\"color: #ff0000\"><strong>xmlns=<\/strong><\/span><\/span><\/span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d\"><span style=\"font-family: Segoe UI;color: #000000\"><strong><span style=\"color: #ff0000\">&#8220;<\/span><\/strong><\/span><\/span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d\"><span style=\"font-family: Segoe UI;color: #000000\"><span style=\"color: #ff0000\"><strong>hello<\/strong><\/span><\/span><\/span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d\"><span style=\"font-family: Segoe UI;color: #000000\"><strong><span style=\"color: #ff0000\">&#8220;<\/span><\/strong><\/span><\/span><\/span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d;font-size: 11pt\"><span style=\"font-family: Segoe UI;color: #000000\"><span style=\"font-size: small\">&gt;&lt;\/B&gt; <br \/>&lt;\/A&gt;<\/span><\/span><\/span><\/span><\/p>\n<p><span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d;font-size: 11pt\"><\/span><\/span><span><span style=\"font-size: small\">As you can see, in the output for variable y, the element &lt;B&gt; has a spurious (duplicate) XML namespace declaration. Although this XML is technically &lsquo;correct&rsquo; (i.e. legal XML according to the <\/span><a href=\"http:\/\/www.w3.org\/TR\/REC-xml\/\"><span style=\"font-size: small\">XML 1.0 specification<\/span><\/a><span style=\"font-size: small\">), several tools have problems consuming XML that contains duplicate namespaces. <\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d\"><span style=\"font-size: small\"><\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe UI;color: #000000\"><span style=\"font-size: small\">For example, consider the below code where I load an MSBuild project file (.vbproj) and try to modify its contents. MSBuild projects are essentially just XML files, so I can use VB XML literals to work with such files. I have set the default XML namespace for my code to match the namespace of the XML in the MSBuild file. Notice that the output has the same problem as before &ndash; the &lt;NoWarn&gt; node that I added has a duplicate XML namespace. <\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe UI;color: #000000\"><span style=\"font-size: small\">If I were to save the XML produced by this code as a &ldquo;.vbproj&rdquo; file and build it using MSBuild, MSBuild would fail to process the file because\nof the duplicate namesapace on the &lt;NoWarn&gt; element. In other words, even though the XML is legal, MSBuild can&rsquo;t consume XML that contains duplicate namespaces. <\/p>\n<p><span style=\"text-decoration: underline\">Code<\/span>: <br \/><\/span><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\"><br \/>Imports<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> &lt;<span style=\"color: #b96464\">xmlns<\/span><span style=\"color: #6464b9\">=<\/span><span style=\"color: #555555\">&#8220;<\/span><span style=\"color: #6464b9\">http:\/\/schemas.microsoft.com\/developer\/msbuild\/2003<\/span><span style=\"color: #555555\">&#8220;<\/span>&gt; <\/p>\n<p><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\">Module<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: #2b91af\">Module2 <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Sub<\/span> Main() <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Dim<\/span> projectFile <span style=\"color: blue\">As<\/span> <span style=\"color: blue\">String<\/span> = <\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span style=\"color: #a31515\">&#8220;&#8230;.ConsoleApplication1.vbproj&#8221; <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Dim<\/span> y = <span style=\"color: #2b91af\">XDocument<\/span>.Load(projectFile) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span style=\"color: blue\">Dim<\/span> element = <\/span>&nbsp;<span style=\"font-family: consolas;font-size: 9.5pt\"><span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">NoWarn<\/span><span style=\"color: #6464b9\">&gt;42016<\/span><span style=\"color: #6464b9\">&lt;\/<\/span><span style=\"color: #844646\">NoWarn<\/span><span style=\"color: #6464b9\">&gt;<\/span><\/span>&nbsp;<span style=\"font-family: consolas;font-size: 9.5pt\"> <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>y<span style=\"color: #6464b9\">.&lt;<\/span>Project<span style=\"color: #6464b9\">&gt;.&lt;<\/span>PropertyGroup<span style=\"color: #6464b9\">&gt;<\/span>.First.Add(element) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">Console<\/span>.WriteLine(<span style=\"color: #a31515\">&#8220;y:&#8221;<\/span>) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">Console<\/span>.WriteLine(y) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">End<\/span> <span style=\"color: blue\">Sub <br \/><\/span><\/span><span style=\"line-height: 115%;font-family: consolas;color: blue;font-size: 9.5pt\">End<\/span><span style=\"line-height: 115%;font-family: consolas;font-size: 9.5pt\"> <span style=\"color: blue\">Module<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"text-decoration: underline\">Output<\/span>:<\/span><\/p>\n<p><span style=\"font-size: small\">y: <br \/>&lt;Project ToolsVersion=&#8221;4.0&#8243; DefaultTargets=&#8221;Build&#8221; <span style=\"color: #ff0000\"><strong>xmlns=&#8221;<\/strong><\/span><\/span><a href=\"http:\/\/schemas.microsoft.com\/developer\/msbuild\/2003\"><span><strong><span style=\"font-size: small\">http:\/\/schemas.microsoft.com\/developer\/msbuild\/2003<\/span><\/strong><\/span><\/a><span style=\"font-size: small\"><span style=\"color: #ff0000\"><strong>&#8220;<\/strong><\/span>&gt; <br \/>&nbsp; &lt;PropertyGroup&gt; <br \/>&hellip; <br \/>&nbsp;&nbsp;&nbsp; &lt;NoWarn <strong><span style=\"color: #ff0000\">xmlns=&#8221;<\/span><\/strong><\/span><a href=\"http:\/\/schemas.microsoft.com\/developer\/msbuild\/2003%22&gt;42016&lt;\/NoWarn\"><strong><span style=\"color: #ff0000\"><span style=\"font-size: small\">420164201642016http:\/\/schemas.microsoft.com\/developer\/msbuild\/2003&#8243;<\/span><\/span><\/strong><\/a><a><\/a><a><\/a><span><span style=\"font-size: small\">&gt;<span style=\"color: #000000\">42016<\/span><\/span><a><\/a><span style=\"font-size: small\">&lt;\/NoWarn&gt; <br \/>&nbsp; &lt;\/PropertyGroup&gt; <br \/>&hellip; <br \/>&lt;\/Project&gt;<\/span><\/span><a><\/a><\/p>\n<p><span style=\"font-size: small\"><\/span><\/p>\n<p><span><span style=\"font-size: small\"><strong>Why does VB allow XML with duplicate namespaces to be generated? <br \/><\/strong><br \/>Consider the first code example above. As you can see from the output for variable x, the VB compiler correctly figures out that a namespace declaration need not be emitted on node &lt;B&gt;. The compiler can figure this out because node &lt;B&gt; is &lsquo;syntactically&rsquo; embedded (using an <\/span><a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb384964.aspx\" rel=\"noopener\"><span style=\"font-size: small\">embedded expression<\/span><\/a><span style=\"font-size: small\">) inside node &lt;A&gt; which is already in the same namespace (i.e. &ldquo;hello&rdquo;).<\/span><\/span><\/p>\n<p><span style=\"font-size: small\">For variable y however, it is very hard for the VB compiler to know that the node &lt;B&gt; is actually going to be embedded inside node &lt;A&gt;. To know this, the compiler would have to inspect the code flow and try to figure out that the node &lt;B&gt; is being passed to an function named &lsquo;Add&rsquo; that is defined on type &lsquo;XElement&rsquo; and that the target object for function (i.e. y) actually holds a node &lt;A&gt; that is already in the same namespace (i.e. &ldquo;hello&rdquo;). Even if the compiler were smart enough to figure this out for this case, it would be almost impossible to make it smart enough to figure this out for cases (like the second code example above) where the source XML is not part of the program (but comes from some file \/ network packet). <\/span><\/p>\n<p><span style=\"font-size: small\">Because the compiler doesn&rsquo;t know what document the node &lt;B&gt; is going to end up inside, it &lsquo;fully qualifies&rsquo; it with the default namespace of the code file (i.e. &ldquo;hello&rdquo;).<\/span><\/p>\n<p><span><strong><span style=\"font-size: small\">Ok so the VB compiler can&rsquo;t figure this out. Surely the LINQ to XML API can, can&rsquo;t it?<\/span><\/strong><\/span><\/p>\n<p><span style=\"font-size: small\">Yes, the LINQ to XML API can figure out and remove duplicate namespaces. But it does not enforce the removal of duplicate namespaces by default. I think the reason it doesn&rsquo;t is performance (i.e. there is a performance hit involved in checking each node to see whether the node has any duplicate namespaces). After all, the XML is legal (albeit a bit ugly) even when it has duplicate namespaces &ndash; so why force an extra namespace check always?<\/span><\/p>\n<p><span style=\"font-size: small\">In VS 2010 \/ .NET 4.0, the LINQ to XML API provides ways to work-around this problem and generate better looking XML. You can add an &lsquo;annotation&rsquo; to the root XElement \/ XDocument node that will tell the API not to emit duplicate namespaces as demonstrated in the below example. The API will then check each node as it is added and remove any unnecessary duplicate namespace declarations from the node. <\/span><\/p>\n<p><span style=\"font-size: small\">Alternately, you can use &lsquo;SaveOptions&rsquo; \/\n &lsquo;ReaderOptions&rsquo; as demonstrated in the examples below. In this case, the XML will be generated with duplicate namespaces, but the API will do extra work to remove the duplicate namespaces at the time of saving the XML.<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"text-decoration: underline\">Code<\/span>: <br \/><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\"><br \/>Imports<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> &lt;<span style=\"color: #b96464\">xmlns<\/span><span style=\"color: #6464b9\">=<\/span><span style=\"color: #555555\">&#8220;<\/span><span style=\"color: #6464b9\">hello<\/span><span style=\"color: #555555\">&#8220;<\/span>&gt;<\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;<\/span> <\/p>\n<p><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\">Module<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: #2b91af\">Module1 <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">Sub<\/span> Main() <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">Dim<\/span> y = <span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">A<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">A<\/span><span style=\"color: #6464b9\">&gt; <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"background: yellow\">y.AddAnnotation(<span style=\"color: #2b91af\">SaveOptions<\/span>.OmitDuplicateNamespaces) <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y.Add(<span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;<\/span>) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y.Add(<span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">C<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">C<\/span><span style=\"color: #6464b9\">&gt;<\/span>) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af\">Console<\/span>.WriteLine(<span style=\"color: #a31515\">&#8220;y:&#8221;<\/span>) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af\">Console<\/span>.WriteLine(y) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">End<\/span> <span style=\"color: blue\">Sub <br \/><\/span><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\">End<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: blue\">Module<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;margin-bottom: 0pt\"><span style=\"font-family: consolas;color: green;font-size: 9.5pt\">&#8216;If you wish to save the XML to a file <br \/><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\">Module<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: #2b91af\">Module2 <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">Sub<\/span> Main() <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">Dim<\/span> y = <span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">A<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">A<\/span><span style=\"color: #6464b9\">&gt; <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y.Add(<span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;<\/span>) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y.Add(<span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">C<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">C<\/span><span style=\"color: #6464b9\">&gt;<\/span>) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"font-family: consolas;background: yellow;font-size: 9.5pt\">y.Save(<span style=\"color: #a31515\">&#8220;out.xml&#8221;<\/span>, <span style=\"color: #2b91af\">SaveOptions<\/span>.OmitDuplicateNamespaces)<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">End<\/span> <span style=\"color: blue\">Sub <br \/><\/span><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\">End<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: blue\">Module<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;margin-bottom: 0pt\"><span style=\"font-family: consolas;color: green;font-size: 9.5pt\">&#8216;If you wish to create an XmlReader object for your XML <br \/><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\">Module<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: #2b91af\">Module1 <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">Sub<\/span> Main() <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">Dim<\/span> y = <span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">A<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">A<\/span><span style=\"color: #6464b9\">&gt; <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y.Add(<span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">B<\/span><span style=\"color: #6464b9\">&gt;<\/span>) <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y.Add(<span style=\"color: #6464b9\">&lt;<\/span><span style=\"color: #844646\">C<\/span><span style=\"color: #6464b9\">&gt;&lt;\/<\/span><span style=\"color: #844646\">C<\/span><span style=\"color: #6464b9\">&gt;<\/span>) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span style=\"color: blue\">Dim<\/span> reader = <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"background: yellow\">y.CreateReader(<span style=\"color: #2b91af\">ReaderOptions<\/span>.OmitDuplicateNamespaces) <br \/><\/span><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&hellip; <br \/><\/span><span style=\"font-family: consolas;font-size: 9.5pt\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue\">End<\/span> <span style=\"color: blue\">Sub <br \/><\/span><\/span><span style=\"font-family: consolas;color: blue;font-size: 9.5pt\">End<\/span><span style=\"font-family: consolas;font-size: 9.5pt\"> <span style=\"color: blue\">Module<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"text-decoration: underline\">Output<\/span>:&nbsp; <\/p>\n<p>y: <br \/><\/span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d;font-size: 11pt\"><span style=\"font-family: Segoe UI;color: #000000\"><span style=\"font-size: small\">&lt;A xmlns=&#8221;hello&#8221;&gt; <br \/>&nbsp; &lt;B&gt;&lt;\/B&gt; <br \/><\/span><\/span><\/span><span style=\"font-size: small\"><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d\"><span style=\"font-family: Segoe UI;color: #000000\">&nbsp; &lt;C&gt;&lt;\/C&gt;<\/span> <br \/><\/span><span style=\"font-family: 'Calibri','sans-serif';color: #1f497d\"><span style=\"font-family: Segoe UI;color: #000000\">&lt;\/A&gt;<\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\">Hope this helps clean up the XML for your apps! \ud83d\ude42<\/span><\/p>\n<p><span><span style=\"text-decoration: underline\"><br \/><span style=\"font-size: small\">Some references from MSDN<\/span><\/span><span style=\"font-size: small\">: <\/p>\n<p><\/span><\/span><span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb538297(VS.95).aspx\"><span style=\"font-size: small\">XDocument.Save Method<\/span><\/a><span style=\"font-size: small\"> <br \/><\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.linq.saveoptions(VS.95).aspx\"><span style=\"font-size: small\">SaveOptions Enumeration<\/span><\/a><span style=\"font-size: small\"> <br \/><\/span><\/span><span><a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb384589.aspx\" rel=\"noopener\"><span style=\"font-size: small\">Imports Statement (XML Namespace)<\/span><\/a><span style=\"font-size: small\"> <\/span><\/span><span><br \/><\/span><\/p>\n<p><span><span style=\"font-size: small\"><span style=\"text-decoration: underline\">Ways to remove duplicate namespaces before VB 2010<\/span>:<\/p>\n<p>Bill McCarthy&nbsp;has a couple of blog posts about&nbsp;how you can clean up namespaces if you are&nbsp;using VS 2008 \/ .NET 3.5&nbsp;&#8211;<br \/><\/span><a href=\"http:\/\/msmvps.com\/blogs\/bill\/archive\/2007\/12\/09\/more-on-xml-namespaces-in-vb.aspx\"><span style=\"font-size: small\">http:\/\/msmvps.com\/blogs\/bill\/archive\/2007\/12\/09\/more-on-xml-namespaces-in-vb.aspx<\/span><\/a><br \/><a href=\"http:\/\/msmvps.com\/blogs\/bill\/archive\/2007\/11\/24\/cleaning-up-your-xml-literal-namespaces.aspx\"><span style=\"font-size: small\">http:\/\/msmvps.com\/blogs\/bill\/archive\/2007\/11\/24\/cleaning-up-your-xml-literal-namespaces.aspx<\/span><\/a><\/span><span><br \/><\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common problem that one often runs into with XML literals and the LINQ to XML API is duplicate XML namespaces. Consider the following example. The code imports a default XML namespace &#8211; &ldquo;hello&rdquo;. Code: Imports &lt;xmlns=&#8220;hello&#8220;&gt; Module Module1 &nbsp;&nbsp;&nbsp; Sub Main() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim x = &lt;A&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%= &lt;B&gt;&lt;\/B&gt; %&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/A&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&#8220;x:&#8221;) [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[195],"tags":[136,167,185],"class_list":["post-833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-basic","tag-shyam-namboodiripad","tag-vb2010","tag-xml"],"acf":[],"blog_post_summary":"<p>A common problem that one often runs into with XML literals and the LINQ to XML API is duplicate XML namespaces. Consider the following example. The code imports a default XML namespace &#8211; &ldquo;hello&rdquo;. Code: Imports &lt;xmlns=&#8220;hello&#8220;&gt; Module Module1 &nbsp;&nbsp;&nbsp; Sub Main() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim x = &lt;A&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%= &lt;B&gt;&lt;\/B&gt; %&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/A&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&#8220;x:&#8221;) [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/833\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}