More than one attribute.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RGF2ZQ==?=

    More than one attribute.

    you can write
    writer.WriteSta rtElement("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:noNamespace SchemaLocation= "../documents/xml/epic.xsd">

    for two attributes?


  • Martin Honnen

    #2
    Re: More than one attribute.

    Dave wrote:
    you can write
    writer.WriteSta rtElement("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:noNamespace SchemaLocation= "../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.Creat e(Console.Out))
    {
    writer.WriteSta rtElement("data ");
    writer.WriteAtt ributeString("x si",
    "noNamespaceSch emaLocation", xsi, "../documents/xml/epic.xsd");
    writer.WriteEnd Element();
    }

    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.Creat e(Console.Out))
    {
    writer.WriteSta rtElement("data ");
    writer.WriteAtt ributeString("x mlns", "xsi",
    "http://www.w3.org/2000/xmlns/", xsi);
    writer.WriteAtt ributeString("x si",
    "noNamespaceSch emaLocation", xsi, "../documents/xml/epic.xsd");
    writer.WriteEnd Element();
    }

    --

    Martin Honnen --- MVP XML

    Comment

    • =?Utf-8?B?RGF2ZQ==?=

      #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:
      Dave wrote:
      you can write
      writer.WriteSta rtElement("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:noNamespace SchemaLocation= "../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.Creat e(Console.Out))
      {
      writer.WriteSta rtElement("data ");
      writer.WriteAtt ributeString("x si",
      "noNamespaceSch emaLocation", xsi, "../documents/xml/epic.xsd");
      writer.WriteEnd Element();
      }
      >
      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.Creat e(Console.Out))
      {
      writer.WriteSta rtElement("data ");
      writer.WriteAtt ributeString("x mlns", "xsi",
      "http://www.w3.org/2000/xmlns/", xsi);
      writer.WriteAtt ributeString("x si",
      "noNamespaceSch emaLocation", xsi, "../documents/xml/epic.xsd");
      writer.WriteEnd Element();
      }
      >
      --
      >
      Martin Honnen --- MVP XML

      >

      Comment

      • Martin Honnen

        #4
        Re: More than one attribute.

        Dave wrote:
        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.QuoteCha r = '\'';

        --

        Martin Honnen --- MVP XML

        Comment

        Working...