Formatting XmlTextWriter Output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • clintonG

    Formatting XmlTextWriter Output

    // I want this format...
    <rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:media="ht tp://search.yahoo.co m/mrss/">

    The WriteStartEleme nt is used for the rss element and extended using the
    WriteAttributeS tring method which outputs the version and namespace
    attributes. Can someone tell me how to format the output as shown above?

    <%= Clinton Gallagher
    NET csgallagher AT metromilwaukee. com
    URL http://clintongallagher.metromilwaukee.com/



  • Martin Honnen

    #2
    Re: Formatting XmlTextWriter Output

    clintonG wrote:
    // I want this format...
    <rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:media="ht tp://search.yahoo.co m/mrss/">
    >
    The WriteStartEleme nt is used for the rss element and extended using the
    WriteAttributeS tring method which outputs the version and namespace
    attributes. Can someone tell me how to format the output as shown above?
    With .NET 2.0 you can use
    XmlWriterSettin gs writerSettings = new XmlWriterSettin gs();
    writerSettings. NewLineOnAttrib utes = true;
    writerSettings. Indent = true;
    then use those settings to create your writer e.g.
    using (XmlWriter writer = XmlWriter.Creat e(@"file.xml", writerSettings) )
    {
    // write XML here
    }
    That will not give exactly what you have specified above I think but at
    least each attribute should be written on a line of its own.



    --

    Martin Honnen --- MVP XML

    Comment

    • clintonG

      #3
      Re: Formatting XmlTextWriter Output

      That's something to work with thanks...

      <%= Clinton

      "Martin Honnen" <mahotrash@yaho o.dewrote in message
      news:urywyXSfHH A.208@TK2MSFTNG P05.phx.gbl...
      clintonG wrote:
      >// I want this format...
      ><rss version="2.0"
      > xmlns:dc="http://purl.org/dc/elements/1.1/"
      > xmlns:media="ht tp://search.yahoo.co m/mrss/">
      >>
      >The WriteStartEleme nt is used for the rss element and extended using the
      >WriteAttribute String method which outputs the version and namespace
      >attributes. Can someone tell me how to format the output as shown above?
      >
      With .NET 2.0 you can use
      XmlWriterSettin gs writerSettings = new XmlWriterSettin gs();
      writerSettings. NewLineOnAttrib utes = true;
      writerSettings. Indent = true;
      then use those settings to create your writer e.g.
      using (XmlWriter writer = XmlWriter.Creat e(@"file.xml", writerSettings) )
      {
      // write XML here
      }
      That will not give exactly what you have specified above I think but at
      least each attribute should be written on a line of its own.
      >
      >
      >
      --
      >
      Martin Honnen --- MVP XML
      http://JavaScript.FAQTs.com/

      Comment

      • clintonG

        #4
        Re: Formatting XmlTextWriter Output

        Amazing!
        The inclusion of the XmlWriterSettin gs object will not allow any string
        arguments that contain a colon.

        // this will now barf...
        writer.WriteAtt ributeString("x mlns:dc", "http://purl.org/dc/elements/1.1/");

        <%= Clinton



        "Martin Honnen" <mahotrash@yaho o.dewrote in message
        news:urywyXSfHH A.208@TK2MSFTNG P05.phx.gbl...
        clintonG wrote:
        >// I want this format...
        ><rss version="2.0"
        > xmlns:dc="http://purl.org/dc/elements/1.1/"
        > xmlns:media="ht tp://search.yahoo.co m/mrss/">
        >>
        >The WriteStartEleme nt is used for the rss element and extended using the
        >WriteAttribute String method which outputs the version and namespace
        >attributes. Can someone tell me how to format the output as shown above?
        >
        With .NET 2.0 you can use
        XmlWriterSettin gs writerSettings = new XmlWriterSettin gs();
        writerSettings. NewLineOnAttrib utes = true;
        writerSettings. Indent = true;
        then use those settings to create your writer e.g.
        using (XmlWriter writer = XmlWriter.Creat e(@"file.xml", writerSettings) )
        {
        // write XML here
        }
        That will not give exactly what you have specified above I think but at
        least each attribute should be written on a line of its own.
        >
        >
        >
        --
        >
        Martin Honnen --- MVP XML
        http://JavaScript.FAQTs.com/

        Comment

        • Martin Honnen

          #5
          Re: Formatting XmlTextWriter Output

          clintonG wrote:
          // this will now barf...
          writer.WriteAtt ributeString("x mlns:dc", "http://purl.org/dc/elements/1.1/");
          You need to use
          write.WriteAttr ibuteString(
          "xmlns",
          "df",
          "http://www.w3.org/2000/xmlns/",
          "http://purl.org/dc/elements/1.1/"
          );
          or
          write.WriteAttr ibuteString(
          "xmlns",
          "df",
          null,
          "http://purl.org/dc/elements/1.1/"
          );

          --

          Martin Honnen --- MVP XML

          Comment

          • clintonG

            #6
            Re: Formatting XmlTextWriter Output

            I try to read documentation but I didn't observe how to use overloaded
            methods in this context. The documentation certainly appears to be
            consistent. Could you tell me what part of the documentation typifys methods
            which can be overloaded? I mean what I need to learn to look for?

            I'll try it out Martin and thanks fro bringing this to my attention...

            <%= Clinton

            "Martin Honnen" <mahotrash@yaho o.dewrote in message
            news:ufollNcfHH A.4364@TK2MSFTN GP06.phx.gbl...
            clintonG wrote:
            >
            >// this will now barf...
            >writer.WriteAt tributeString(" xmlns:dc",
            >"http://purl.org/dc/elements/1.1/");
            >
            You need to use
            write.WriteAttr ibuteString(
            "xmlns",
            "df",
            "http://www.w3.org/2000/xmlns/",
            "http://purl.org/dc/elements/1.1/"
            );
            or
            write.WriteAttr ibuteString(
            "xmlns",
            "df",
            null,
            "http://purl.org/dc/elements/1.1/"
            );
            >
            --
            >
            Martin Honnen --- MVP XML
            http://JavaScript.FAQTs.com/

            Comment

            • clintonG

              #7
              Re: Formatting XmlTextWriter Output

              No errors but...

              // same single line persists
              <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">

              And...I'm in some relevant documentation at the moment [1] but observe
              undocumented parameters being used, i.e. null and your suggested alternative
              use of "http://www.w3.org/2000/xmlns/" both of which I've tried but still
              get a single line result.

              // As documented [1] the method takes two and only two arguments
              // XmlWriter.Write AttributeString Method (String, String)
              public void WriteAttributeS tring (
              string localName,
              string value
              )

              <%= Clinton

              [1] http://msdn2.microsoft.com/en-us/lib...d8(vs.80).aspx

              "clintonG" <nobody@nowhere .comwrote in message
              news:Ovuo0AefHH A.3960@TK2MSFTN GP02.phx.gbl...
              >I try to read documentation but I didn't observe how to use overloaded
              >methods in this context. The documentation certainly appears to be
              >consistent. Could you tell me what part of the documentation typifys
              >methods which can be overloaded? I mean what I need to learn to look for?
              >
              I'll try it out Martin and thanks fro bringing this to my attention...
              >
              <%= Clinton
              >
              "Martin Honnen" <mahotrash@yaho o.dewrote in message
              news:ufollNcfHH A.4364@TK2MSFTN GP06.phx.gbl...
              >clintonG wrote:
              >>
              >>// this will now barf...
              >>writer.WriteA ttributeString( "xmlns:dc",
              >>"http://purl.org/dc/elements/1.1/");
              >>
              >You need to use
              > write.WriteAttr ibuteString(
              > "xmlns",
              > "df",
              > "http://www.w3.org/2000/xmlns/",
              > "http://purl.org/dc/elements/1.1/"
              > );
              >or
              > write.WriteAttr ibuteString(
              > "xmlns",
              > "df",
              > null,
              > "http://purl.org/dc/elements/1.1/"
              > );
              >>
              >--
              >>
              >Martin Honnen --- MVP XML
              >http://JavaScript.FAQTs.com/
              >
              >

              Comment

              • clintonG

                #8
                Re: Formatting XmlTextWriter Output

                Got the documentation and parameter list issue resolved. Still left with
                trying to resolve why the XmlWriterSettin gs object properties are not being
                applied.

                <%= Clinton



                "clintonG" <nobody@nowhere .comwrote in message
                news:O08xzsefHH A.444@TK2MSFTNG P04.phx.gbl...
                No errors but...
                >
                // same single line persists
                <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
                >
                And...I'm in some relevant documentation at the moment [1] but observe
                undocumented parameters being used, i.e. null and your suggested
                alternative use of "http://www.w3.org/2000/xmlns/" both of which I've
                tried but still get a single line result.
                >
                // As documented [1] the method takes two and only two arguments
                // XmlWriter.Write AttributeString Method (String, String)
                public void WriteAttributeS tring (
                string localName,
                string value
                )
                >
                <%= Clinton
                >
                [1] http://msdn2.microsoft.com/en-us/lib...d8(vs.80).aspx
                >
                "clintonG" <nobody@nowhere .comwrote in message
                news:Ovuo0AefHH A.3960@TK2MSFTN GP02.phx.gbl...
                >>I try to read documentation but I didn't observe how to use overloaded
                >>methods in this context. The documentation certainly appears to be
                >>consistent. Could you tell me what part of the documentation typifys
                >>methods which can be overloaded? I mean what I need to learn to look for?
                >>
                >I'll try it out Martin and thanks fro bringing this to my attention...
                >>
                ><%= Clinton
                >>
                >"Martin Honnen" <mahotrash@yaho o.dewrote in message
                >news:ufollNcfH HA.4364@TK2MSFT NGP06.phx.gbl.. .
                >>clintonG wrote:
                >>>
                >>>// this will now barf...
                >>>writer.Write AttributeString ("xmlns:dc",
                >>>"http://purl.org/dc/elements/1.1/");
                >>>
                >>You need to use
                >> write.WriteAttr ibuteString(
                >> "xmlns",
                >> "df",
                >> "http://www.w3.org/2000/xmlns/",
                >> "http://purl.org/dc/elements/1.1/"
                >> );
                >>or
                >> write.WriteAttr ibuteString(
                >> "xmlns",
                >> "df",
                >> null,
                >> "http://purl.org/dc/elements/1.1/"
                >> );
                >>>
                >>--
                >>>
                >>Martin Honnen --- MVP XML
                >>http://JavaScript.FAQTs.com/
                >>
                >>
                >
                >

                Comment

                • Martin Honnen

                  #9
                  Re: Formatting XmlTextWriter Output

                  clintonG wrote:
                  No errors but...
                  >
                  // same single line persists
                  <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
                  I am not sure why the xmlns declarations end up on the same line, that
                  seems a bug to me. Or the writers of the XmlWriter API treat xmlns
                  declaration attributes intentionally different although I can't see a
                  reason for that, if NewLineOnAttrib utes is true then that should apply
                  to xmlns attributes as well in my opinion.


                  --

                  Martin Honnen --- MVP XML

                  Comment

                  • clintonG

                    #10
                    Re: Formatting XmlTextWriter Output


                    "Martin Honnen" <mahotrash@yaho o.dewrote in message
                    news:e71eNmpfHH A.1216@TK2MSFTN GP03.phx.gbl...
                    clintonG wrote:
                    >No errors but...
                    >>
                    >// same single line persists
                    ><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
                    >
                    I am not sure why the xmlns declarations end up on the same line, that
                    seems a bug to me. Or the writers of the XmlWriter API treat xmlns
                    declaration attributes intentionally different although I can't see a
                    reason for that, if NewLineOnAttrib utes is true then that should apply to
                    xmlns attributes as well in my opinion.
                    >
                    >
                    --
                    >
                    Martin Honnen --- MVP XML
                    http://JavaScript.FAQTs.com/
                    I'm going to try to replicate with a new file sometime today. With all the
                    brooha about XML being a readable format one would think there would have
                    been more emphasis on formatting the raw XML.

                    It was also interesting to note Intellisense displays a Settings property
                    for an instance of an XmlWriter object but barfs when it is used to try to
                    set a property. I think (what I consider to be a redundant) instance of an
                    XmlWriterSettin gs object is going to be required and will force all
                    attributes in the entire file to be formatted whether wanted or not. If it
                    actually functions fot this purpose. Thanks for paying attention...

                    <%= Clinton


                    Comment

                    • Tom Jackson

                      #11
                      Formatting XmlTextWriter Output

                      So far as I can tell, that is how settings are built and applied - just like you said - they apply to the whole document and I don't believe can be set on an element/attribute basis:

                      XmlWriterSettin gs settings = new XmlWriterSettin gs();
                      settings.Indent = true;
                      XmlWriter xw = XmlWriter.Creat e(filename, settings);

                      What I'm running into is where I want to assign both an attribute and content to an element, i.e. to produce

                      <content encoding="base6 4binary">encode d content here</content>

                      it doesn't like it unless I do a "WriteRaw" for the content:

                      xw.WriteStartEl ement("content" , null);
                      xw.WriteAttribu teString("encod ing", "base64binary") ;
                      xw.WriteRaw(mes sageContent);
                      xw.WriteEndElem ent(); // end the content element

                      This comes out fine, but I was just curious if there was another, better, way?

                      -Tom

                      Comment

                      • Tom Jackson

                        #12
                        Formatting XmlTextWriter Output

                        It didn't like my XML in that post above. Here, again, is what I'm trying to produce:

                        "bracket" content encoding="base6 4encoding" "bracket" encoded content here....
                        "bracket" /content "bracket"

                        Comment

                        • Tom Jackson

                          #13
                          Formatting XmlTextWriter Output

                          3rd time's a charm? Just change out the semi-colons below for brackets. I tried typing "bracket" before, above, and it didn't like it/wouldn't post it.

                          Code:
                          ;content encoded="base64binary";my encoded content here;/content;

                          Comment

                          • Martin Honnen

                            #14
                            Re: Formatting XmlTextWriter Output

                            Tom Jackson wrote:
                            What I'm running into is where I want to assign both an attribute and content to an element, i.e. to produce
                            >
                            <content encoding="base6 4binary">encode d content here</content>
                            >
                            it doesn't like it unless I do a "WriteRaw" for the content:
                            >
                            xw.WriteStartEl ement("content" , null);
                            xw.WriteAttribu teString("encod ing", "base64binary") ;
                            xw.WriteRaw(mes sageContent);
                            xw.WriteEndElem ent(); // end the content element
                            >
                            This comes out fine, but I was just curious if there was another, better, way?
                            What does "it doesn't like it" mean exactly? Which code exactly have you
                            tried? Which error exactly did you get?

                            --

                            Martin Honnen --- MVP XML

                            Comment

                            Working...