Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

More than one attribute.

Question posted by: =?Utf-8?B?RGF2ZQ==?= (Guest) on June 27th, 2008 07:20 PM
you can write
writer.WriteStartElement("data", "http://www.w3.org/1999/XMLSchema-instance");

in your code for,

<data xmlns="http://www.w3.org/1999/XMLSchema-instance">

how do you write,

<data xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../documents/xml/epic.xsd">

for two attributes?


Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:20 PM
#2

Re: More than one attribute.
Dave wrote:
Quote:
you can write
writer.WriteStartElement("data", "http://www.w3.org/1999/XMLSchema-instance");
>
in your code for,
>
<data xmlns="http://www.w3.org/1999/XMLSchema-instance">
>
how do you write,
>
<data xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../documents/xml/epic.xsd">
>
for two attributes?


Note that the official W3C schema instance URI is
http://www.w3.org/2001/XMLSchema-instance.

As for writing attributes, in case of a namespace declaration like
xmlns:xsi="..." you don't need to write it at all, it suffices to write
the attribute in that namespace:


const string xsi = "http://www.w3.org/2001/XMLSchema-instance";
using (XmlWriter writer = XmlWriter.Create(Console.Out))
{
writer.WriteStartElement("data");
writer.WriteAttributeString("xsi",
"noNamespaceSchemaLocation", xsi, "../documents/xml/epic.xsd");
writer.WriteEndElement();
}

If you want to explicitly write the namespace declaration, then that is
possible too:

const string xsi = "http://www.w3.org/2001/XMLSchema-instance";
using (XmlWriter writer = XmlWriter.Create(Console.Out))
{
writer.WriteStartElement("data");
writer.WriteAttributeString("xmlns", "xsi",
"http://www.w3.org/2000/xmlns/", xsi);
writer.WriteAttributeString("xsi",
"noNamespaceSchemaLocation", xsi, "../documents/xml/epic.xsd");
writer.WriteEndElement();
}

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

=?Utf-8?B?RGF2ZQ==?='s Avatar
=?Utf-8?B?RGF2ZQ==?=
Guest
n/a Posts
June 27th, 2008
07:20 PM
#3

Re: More than one attribute.
Thanks, that exactly what I needed.

One more question, the customer where we're sending the files to says they
can't read double quotes around attributes, is there a way to specify single
quotes?

"Martin Honnen" wrote:
Quote:
Dave wrote:
Quote:
you can write
writer.WriteStartElement("data", "http://www.w3.org/1999/XMLSchema-instance");

in your code for,

<data xmlns="http://www.w3.org/1999/XMLSchema-instance">

how do you write,

<data xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../documents/xml/epic.xsd">

for two attributes?

>
Note that the official W3C schema instance URI is
http://www.w3.org/2001/XMLSchema-instance.
>
As for writing attributes, in case of a namespace declaration like
xmlns:xsi="..." you don't need to write it at all, it suffices to write
the attribute in that namespace:
>
>
const string xsi = "http://www.w3.org/2001/XMLSchema-instance";
using (XmlWriter writer = XmlWriter.Create(Console.Out))
{
writer.WriteStartElement("data");
writer.WriteAttributeString("xsi",
"noNamespaceSchemaLocation", xsi, "../documents/xml/epic.xsd");
writer.WriteEndElement();
}
>
If you want to explicitly write the namespace declaration, then that is
possible too:
>
const string xsi = "http://www.w3.org/2001/XMLSchema-instance";
using (XmlWriter writer = XmlWriter.Create(Console.Out))
{
writer.WriteStartElement("data");
writer.WriteAttributeString("xmlns", "xsi",
"http://www.w3.org/2000/xmlns/", xsi);
writer.WriteAttributeString("xsi",
"noNamespaceSchemaLocation", xsi, "../documents/xml/epic.xsd");
writer.WriteEndElement();
}
>
--
>
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
>


Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:20 PM
#4

Re: More than one attribute.
Dave wrote:
Quote:
One more question, the customer where we're sending the files to says they
can't read double quotes around attributes, is there a way to specify single
quotes?


XML allows both double and single quotes, if you have agreed to exchange
XML then whoever consumes that should be able to deal with double quotes.

If you really want to change that then you will need to use
XmlTextWriter and set its QuoteChar property as needed:
XmlTextWriter writer = new XmlTextWriter("file.xml", Encoding.UTF8);
writer.QuoteChar = '\'';

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

 
Not the answer you were looking for? Post your question . . .
189,069 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors