Connecting Tech Pros Worldwide Forums | Help | Site Map

XmlDocument xsi: Problem

Mark Jerde
Guest
 
Posts: n/a
#1: Nov 12 '05
I've been looking through MSDN, books and googling for a couple hours but
haven't found the solution yet. I'm using CSharp in VS .NET 2003.

I'm trying to use XmlDocument to save XML files that are valid to a schema
I've developed. I want the root element to look like this:

<TestCase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="TestCaseSchema_03.x sd">

I've tried several variations of XmlElement.SetAttribute() and "
XmlAttribute att = [XmlDocument].CreateAttribute() " The closest I've
gotten to the desired output is (notice the missing "xsi:"):

<TestCase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
noNamespaceSchemaLocation="TestCaseSchema_03.xsd">

Many of the things I've tried look more or less like this:

<TestCase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
d1p1:noNamespaceSchemaLocation="" xmlns:d1p1="TestCaseSchema_03.xsd">

I guess I don't understand DOM well enough to know why these are the
results. Any help will be greatly appreciated!

Thanks.

-- Mark



Kevin Yu [MSFT]
Guest
 
Posts: n/a
#2: Nov 12 '05

re: XmlDocument xsi: Problem


Hi Mark,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to create a root node like
<TestCase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="TestCaseSchema_03.x sd">. If there is any
misunderstanding, please feel free to let me know.

In this case, we can first create an attribute node using
XmlDocument.CreateAttribute() and then add it to the element using
XmlElement.SetAttributeNode() method. Here I wrote some sample code. HTH.

XmlDocument doc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

XmlElement ele = doc.CreateElement("TestCase");
XmlAttribute att = doc.CreateAttribute("xsi", "noNamespaceSchemaLocation",
"http://www.w3.org/2001/XMLSchema-instance");
att.Value = "TestCaseSchema_03.xsd";
ele.SetAttributeNode(att);
doc.AppendChild(ele);

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Mark Jerde
Guest
 
Posts: n/a
#3: Nov 12 '05

re: XmlDocument xsi: Problem


Kevin -- Works great. Thanks! I was missing the XmlNamespaceManager and
att.Value.

-- Mark


Kevin Yu [MSFT]
Guest
 
Posts: n/a
#4: Nov 12 '05

re: XmlDocument xsi: Problem


You're welcome, Mark.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Closed Thread