Resolved "xsi" missing from schema location

ram_rocks

Active member
Joined
Jun 14, 2021
Messages
27
Programming Experience
1-3
I am creating an XMLDocument
xsi prefix is missing from the schema location when I save the file

My input is :
C#:
XmlElement rootnode = xml.CreateElement("Package");
rootnode.SetAttribute("xmlns", "http://www/org/2007");
rootnode.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
rootnode.SetAttribute("xsi:schemaLocation", "http://www.org/schema/.xsd");

I want output like below
C#:
<Package xmlns="http://www/org/2007" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.org/schema/.xsd">
but I am getting as
C#:
<Package xmlns="http://www/org/2007" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.org/schema/.xsd">   (xsi prefix is missing)

I tried to check solution from google and found one thing in this link xsi missing from schemaLocation and type and I also tried included second parameter
C#:
 rootnode.SetAttribute("xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "http://www.org/schema/.xsd");
and I am getting error "The ':' character, hexadecimal value 0x3A, cannot be included in a name"

Can anyone please help me out with this issue ...
 
Last edited by a moderator:
In broad strokes the approach is to use the XmlNameSpaceManager to add all your namespace prefixes and URIs. Then you pull out the NameTable and pass that into the constructor of the XmlDocument. Then you finally call you 3 parameter variant of SetAttribute passing in just the local name as the first parameter, the URI of the namespace as the second parameter, and then value as the last parameter.
 
In broad strokes the approach is to use the XmlNameSpaceManager to add all your namespace prefixes and URIs. Then you pull out the NameTable and pass that into the constructor of the XmlDocument. Then you finally call you 3 parameter variant of SetAttribute passing in just the local name as the first parameter, the URI of the namespace as the second parameter, and then value as the last parameter.

I tried the approach you mentioned and now I am getting the output as below
<Package xmlns="http://www/org/2007" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" d1p1:schemaLocation="http://www.org/schema/.xsd"> (d1p1 instead of xsi) ??
 
@ram_rocks : Sorry that didn't work. But does it really matter where the prefix is "xsi" or "d1p1"? They are semantically the same.
 
When I followed my outline from post #2, the result I got was:
C#:
<Package  d1p1:schemaLocation="http://www.org/schema/.xsd" xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance" />
 
Apparently, the answer was in the first link in the OP. Just scroll all the way to the bottom of the linked page.
 
Well, that didn't work. I got the same error report in the OP regarding the ":" character being illegal.

But this code seems to work:
C#:
var doc = new XmlDocument();
var root = doc.CreateElement("Package");
root.SetAttribute("xmlns", "http://www/org/2007");
root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.SetAttribute("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "http://www.org/schema/.xsd");
doc.AppendChild(root);
Console.WriteLine(doc.OuterXml);

which produces the output:
XML:
<Package xmlns="http://www/org/2007" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.org/schema/.xsd" />
 
Line 2 and 3 can be combined by creating the element in namespace:
C#:
var root = doc.CreateElement("Package", "http://www/org/2007");
 
Same with Linq to Xml, like shown in example "Create a document that has two namespaces, one with a prefix"
C#:
XNamespace defaultNS = "http://www/org/2007";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
var pkg = new XElement(defaultNS + "Package",
    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
    new XAttribute(xsi + "schemaLocation", "http://www.org/schema/.xsd"));
 
Sorry for the delay in response guys and thank you so much for helping me out
@Skydiver I tried post #9 and it worked
@JohnH I already tried #6 and that too worked -> currently using a method Create Attribute
now shifted to post #9 method which reduces 2 lines ?
 
Back
Top Bottom