Convert Hashtable

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

    Convert Hashtable

    Hi,

    I have a Hashtable of string objects that I need to convert to a XML string.
    Any ideas how to do this?

    Thanks


  • Truong Hong Thi

    #2
    Re: Convert Hashtable


    It depends on the schema of the output XML.
    You could use one of the approach.
    - Use StringBuilder
    - Use XmlWriter
    - Use XmlDocument

    Could you describe the desired output XML schema?

    Thi
    Thoughts and ideas about software development


    Joe wrote:
    Hi,
    >
    I have a Hashtable of string objects that I need to convert to a XML string.
    Any ideas how to do this?
    >
    Thanks

    Comment

    • Joe

      #3
      Re: Convert Hashtable

      Thanks for the response. The desired output is just the key as the element
      with the value. Is StringBuilder the best approach?



      "Truong Hong Thi" <thi1981@gmail. comwrote in message
      news:1161921961 .454551.197340@ i42g2000cwa.goo glegroups.com.. .
      >
      It depends on the schema of the output XML.
      You could use one of the approach.
      - Use StringBuilder
      - Use XmlWriter
      - Use XmlDocument
      >
      Could you describe the desired output XML schema?
      >
      Thi
      Thoughts and ideas about software development

      >
      Joe wrote:
      >Hi,
      >>
      >I have a Hashtable of string objects that I need to convert to a XML
      >string.
      >Any ideas how to do this?
      >>
      >Thanks
      >

      Comment

      • Truong Hong Thi

        #4
        Re: Convert Hashtable

        Hi,

        The StringBuilder is simple and best approach to your requirements.
        I guess the desired output is like this:
        <key>value</key>
        If so, make sure the key is a valid XML identifier.
        The safer way is <item key="key">value </item>.
        Sample code:
        const string pattern = "<item key=\"{0}\">{1} </item>";
        StringBuilder buffer = new StringBuilder(" <table>");
        foreach (DictionaryEntr y entry in theHashTable)
        {
        buffer.AppendFo rmat(pattern, entry.Key, entry.Value);
        }
        buffer.Append(" </table>");
        return buffer.ToString ();

        Hope it helps,
        Thi
        Thoughts and ideas about software development


        Joe wrote:
        Thanks for the response. The desired output is just the key as the element
        with the value. Is StringBuilder the best approach?

        Comment

        • Marc Gravell

          #5
          Re: Convert Hashtable

          IMO, using StringBuilder directly is a fairly sure way of introducing
          an occasional bug... all you need is a reserved xml character in either
          the key or value, and it's broken. That's way too brittle for my
          liking.

          You can, however, do something like below; fairly hard to break this
          via content... also, note that the order of items is not guaranteed by
          Hashtable

          Marc

          Hashtable ht = new Hashtable();
          ht.Add("Test", "Something" );
          ht.Add("Whateve r", "abcf");
          ht.Add("asd", "adsfgaa");

          StringBuilder sb = new StringBuilder() ;
          using (XmlWriter writer = XmlWriter.Creat e(sb))
          {
          writer.WriteSta rtElement("item s");
          foreach (DictionaryEntr y item in ht)
          {
          writer.WriteSta rtElement("item ");
          writer.WriteAtt ributeString("k ey",
          item.Key.ToStri ng());
          writer.WriteVal ue(item.Value);
          writer.WriteEnd Element();
          }
          writer.WriteEnd Element();
          writer.Flush();
          }
          Console.WriteLi ne(sb.ToString( ));

          Comment

          • Truong Hong Thi

            #6
            Re: Convert Hashtable

            Yes, that is safer. XmlWriter should know how to escape illegal
            characters.

            Comment

            • Joe

              #7
              Re: Convert Hashtable

              Thanks for the posts. That makes sense to use XMLWriter and I appreciate
              the help.

              "Marc Gravell" <marc.gravell@g mail.comwrote in message
              news:1161925715 .422869.6790@h4 8g2000cwc.googl egroups.com...
              IMO, using StringBuilder directly is a fairly sure way of introducing
              an occasional bug... all you need is a reserved xml character in either
              the key or value, and it's broken. That's way too brittle for my
              liking.
              >
              You can, however, do something like below; fairly hard to break this
              via content... also, note that the order of items is not guaranteed by
              Hashtable
              >
              Marc
              >
              Hashtable ht = new Hashtable();
              ht.Add("Test", "Something" );
              ht.Add("Whateve r", "abcf");
              ht.Add("asd", "adsfgaa");
              >
              StringBuilder sb = new StringBuilder() ;
              using (XmlWriter writer = XmlWriter.Creat e(sb))
              {
              writer.WriteSta rtElement("item s");
              foreach (DictionaryEntr y item in ht)
              {
              writer.WriteSta rtElement("item ");
              writer.WriteAtt ributeString("k ey",
              item.Key.ToStri ng());
              writer.WriteVal ue(item.Value);
              writer.WriteEnd Element();
              }
              writer.WriteEnd Element();
              writer.Flush();
              }
              Console.WriteLi ne(sb.ToString( ));
              >

              Comment

              Working...