How to parse XML?

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

    How to parse XML?

    Hi,

    I am writing a maintenance site for my department. One of the pages
    displays XML from the database in a GridView. I would like to be able
    to edit the cell, which I can currently do, but parse the contents of
    the cell when updating to make sure what is saved into the databse is
    proper XML.

    Any suggestions on how I could do this would be greatly appreciated.


    Thanks
  • Alexey Smirnov

    #2
    Re: How to parse XML?

    On Aug 5, 9:00 pm, nomad <d.bedg...@o2.c o.ukwrote:
    Hi,
    >
    I am writing a maintenance site for my department.  One of the pages
    displays XML from the database in a GridView.  I would like to be able
    to edit the cell, which I can currently do, but parse the contents of
    the cell when updating to make sure what is saved into the databse is
    proper XML.
    >
    Any suggestions on how I could do this would be greatly appreciated.
    >
    Thanks
    Use the XmlDocument class.

    XmlDocument doc = new XmlDocument();
    string xmlData = .... from the database

    doc.Load(new StringReader(xm lData));

    XmlNodeList nodes = xml.SelectNodes ("//topic");

    string prefix;
    foreach (XmlNode node in nodes)
    {
    ....
    }

    doc.Save(xmlDat a);

    ...save xmlData to the database

    Represents an XML document. You can use this class to load, validate, edit, add, and position XML in a document.

    Comment

    • nomad

      #3
      Re: How to parse XML?

      On Aug 5, 8:50 pm, Alexey Smirnov <alexey.smir... @gmail.comwrote :
      On Aug 5, 9:00 pm, nomad <d.bedg...@o2.c o.ukwrote:
      >
      Hi,
      >
      I am writing a maintenance site for my department.  One of the pages
      displays XML from the database in a GridView.  I would like to be able
      to edit the cell, which I can currently do, but parse the contents of
      the cell when updating to make sure what is saved into the databse is
      proper XML.
      >
      Any suggestions on how I could do this would be greatly appreciated.
      >
      Thanks
      >
      Use the XmlDocument class.
      >
      XmlDocument doc = new XmlDocument();
      string xmlData = .... from the database
      >
      doc.Load(new StringReader(xm lData));
      >
      XmlNodeList nodes = xml.SelectNodes ("//topic");
      >
      string prefix;
      foreach (XmlNode node in nodes)
      {
      ....
      >
      }
      >
      doc.Save(xmlDat a);
      >
      ...save xmlData to the database
      >
      http://msdn.microsoft.com/en-us/libr...ldocument.aspx
      HI,

      Thanks very much for your reply. I'm gathering by your code that by
      doing doc.Load(new StringReader(xm lData)); it is basically parsing the
      XML, and if this fails then I trap that error. If it successds then I
      save the xmlData back into the database?

      THanks for your time.

      Comment

      • Alexey Smirnov

        #4
        Re: How to parse XML?

        On Aug 5, 9:59 pm, nomad <d.bedg...@o2.c o.ukwrote:
        On Aug 5, 8:50 pm, Alexey Smirnov <alexey.smir... @gmail.comwrote :
        >
        >
        >
        >
        >
        On Aug 5, 9:00 pm, nomad <d.bedg...@o2.c o.ukwrote:
        >
        Hi,
        >
        I am writing a maintenance site for my department.  One of the pages
        displays XML from the database in a GridView.  I would like to be able
        to edit the cell, which I can currently do, but parse the contents of
        the cell when updating to make sure what is saved into the databse is
        proper XML.
        >
        Any suggestions on how I could do this would be greatly appreciated.
        >
        Thanks
        >
        Use the XmlDocument class.
        >
        XmlDocument doc = new XmlDocument();
        string xmlData = .... from the database
        >
        doc.Load(new StringReader(xm lData));
        >
        XmlNodeList nodes = xml.SelectNodes ("//topic");
        >
        string prefix;
        foreach (XmlNode node in nodes)
        {
        ....
        >
        }
        >
        doc.Save(xmlDat a);
        >
        ...save xmlData to the database
        >>
        HI,
        >
        Thanks very much for your reply.  I'm gathering by your code that by
        doing doc.Load(new StringReader(xm lData)); it is basically parsing the
        XML, and if this fails then I trap that error.  If it successds then I
        save the xmlData back into the database?
        >
        THanks for your time.- Hide quoted text -
        >
        - Show quoted text -
        I think it would save the XmlDocument... What I forgot to mention is
        about special/escape characters in XML (is it what you trying to avoid
        of?). Look at the following article

        How to locate and replace special characters in an XML file with
        Visual C# .NET


        Hope this helps

        Comment

        • nomad

          #5
          Re: How to parse XML?

          On Aug 5, 9:18 pm, Alexey Smirnov <alexey.smir... @gmail.comwrote :
          On Aug 5, 9:59 pm, nomad <d.bedg...@o2.c o.ukwrote:
          >
          >
          >
          On Aug 5, 8:50 pm, Alexey Smirnov <alexey.smir... @gmail.comwrote :
          >
          On Aug 5, 9:00 pm, nomad <d.bedg...@o2.c o.ukwrote:
          >
          Hi,
          >
          I am writing a maintenance site for my department.  One of the pages
          displays XML from the database in a GridView.  I would like to beable
          to edit the cell, which I can currently do, but parse the contents of
          the cell when updating to make sure what is saved into the databse is
          proper XML.
          >
          Any suggestions on how I could do this would be greatly appreciated..
          >
          Thanks
          >
          Use the XmlDocument class.
          >
          XmlDocument doc = new XmlDocument();
          string xmlData = .... from the database
          >
          doc.Load(new StringReader(xm lData));
          >
          XmlNodeList nodes = xml.SelectNodes ("//topic");
          >
          string prefix;
          foreach (XmlNode node in nodes)
          {
          ....
          >
          }
          >
          doc.Save(xmlDat a);
          >
          ...save xmlData to the database
          >>
          HI,
          >
          Thanks very much for your reply.  I'm gathering by your code that by
          doing doc.Load(new StringReader(xm lData)); it is basically parsing the
          XML, and if this fails then I trap that error.  If it successds then I
          save the xmlData back into the database?
          >
          THanks for your time.- Hide quoted text -
          >
          - Show quoted text -
          >
          I think it would save the XmlDocument... What I forgot to mention is
          about special/escape characters in XML (is it what you trying to avoid
          of?). Look at the following article
          >
          How to locate and replace special characters in an XML file with
          Visual C# .NEThttp://support.microso ft.com/kb/316063
          >
          Hope this helps
          Hi,

          Thanks again for the prompt reply. What I'm basically after is that
          if someone makes a change to the XML in the textbox, I want to verify
          that it is well formed XML before saving it into the database. If
          it's not well formed then I can display a message box, which I have
          already written, explaining to the user that it's not valid XML. They
          can then make the relevant change before saving it.

          Hope that makes sense.

          Really appreciate your help.

          Comment

          • Alexey Smirnov

            #6
            Re: How to parse XML?

            On Aug 5, 10:30 pm, nomad <d.bedg...@o2.c o.ukwrote:
            On Aug 5, 9:18 pm, Alexey Smirnov <alexey.smir... @gmail.comwrote :
            >
            >
            >
            >
            >
            On Aug 5, 9:59 pm, nomad <d.bedg...@o2.c o.ukwrote:
            >
            On Aug 5, 8:50 pm, Alexey Smirnov <alexey.smir... @gmail.comwrote :
            >
            On Aug 5, 9:00 pm, nomad <d.bedg...@o2.c o.ukwrote:
            >
            Hi,
            >
            I am writing a maintenance site for my department.  One of the pages
            displays XML from the database in a GridView.  I would like to be able
            to edit the cell, which I can currently do, but parse the contents of
            the cell when updating to make sure what is saved into the databse is
            proper XML.
            >
            Any suggestions on how I could do this would be greatly appreciated.
            >
            Thanks
            >
            Use the XmlDocument class.
            >
            XmlDocument doc = new XmlDocument();
            string xmlData = .... from the database
            >
            doc.Load(new StringReader(xm lData));
            >
            XmlNodeList nodes = xml.SelectNodes ("//topic");
            >
            string prefix;
            foreach (XmlNode node in nodes)
            {
            ....
            >
            }
            >
            doc.Save(xmlDat a);
            >
            ...save xmlData to the database
            >>
            HI,
            >
            Thanks very much for your reply.  I'm gathering by your code that by
            doing doc.Load(new StringReader(xm lData)); it is basically parsing the
            XML, and if this fails then I trap that error.  If it successds then I
            save the xmlData back into the database?
            >
            THanks for your time.- Hide quoted text -
            >
            - Show quoted text -
            >
            I think it would save the XmlDocument... What I forgot to mention is
            about special/escape characters in XML (is it what you trying to avoid
            of?). Look at the following article
            >
            How to locate and replace special characters in an XML file with
            Visual C# .NEThttp://support.microso ft.com/kb/316063
            >
            Hope this helps
            >
            Hi,
            >
            Thanks again for the prompt reply.  What I'm basically after is that
            if someone makes a change to the XML in the textbox, I want to verify
            that it is well formed XML before saving it into the database.  If
            it's not well formed then I can display a message box, which I have
            already written, explaining to the user that it's not valid XML.  They
            can then make the relevant change before saving it.
            >
            Hope that makes sense.
            >
            Really appreciate your help.- Hide quoted text -
            >
            - Show quoted text -

            If you would edit it in the textbox, then you can simply use the
            XmlTextReader:
            Represents a reader that provides fast, non-cached, forward-only access to XML data. We recommend that you use the XmlReader class instead.


            XmlReader r = new XmlTextReader(n ew StringReader(Te xtBox1.Text));

            try {
            while (r.Read())
            {
            //XML is well-formed
            } catch (Exception e) {
            //error
            }

            And if XML is not syntactically correct, the XML parser will raise an
            error.

            Comment

            • nomad

              #7
              Re: How to parse XML?

              On Aug 5, 10:36 pm, Alexey Smirnov <alexey.smir... @gmail.comwrote :
              On Aug 5, 10:30 pm, nomad <d.bedg...@o2.c o.ukwrote:
              >
              >
              >
              On Aug 5, 9:18 pm, Alexey Smirnov <alexey.smir... @gmail.comwrote :
              >
              On Aug 5, 9:59 pm, nomad <d.bedg...@o2.c o.ukwrote:
              >
              On Aug 5, 8:50 pm, Alexey Smirnov <alexey.smir... @gmail.comwrote :
              >
              On Aug 5, 9:00 pm, nomad <d.bedg...@o2.c o.ukwrote:
              >
              Hi,
              >
              I am writing a maintenance site for my department.  One of the pages
              displays XML from the database in a GridView.  I would like to be able
              to edit the cell, which I can currently do, but parse the contents of
              the cell when updating to make sure what is saved into the databse is
              proper XML.
              >
              Any suggestions on how I could do this would be greatly appreciated.
              >
              Thanks
              >
              Use the XmlDocument class.
              >
              XmlDocument doc = new XmlDocument();
              string xmlData = .... from the database
              >
              doc.Load(new StringReader(xm lData));
              >
              XmlNodeList nodes = xml.SelectNodes ("//topic");
              >
              string prefix;
              foreach (XmlNode node in nodes)
              {
              ....
              >
              }
              >
              doc.Save(xmlDat a);
              >
              ...save xmlData to the database
              >>
              HI,
              >
              Thanks very much for your reply.  I'm gathering by your code thatby
              doing doc.Load(new StringReader(xm lData)); it is basically parsing the
              XML, and if this fails then I trap that error.  If it successds then I
              save the xmlData back into the database?
              >
              THanks for your time.- Hide quoted text -
              >
              - Show quoted text -
              >
              I think it would save the XmlDocument... What I forgot to mention is
              about special/escape characters in XML (is it what you trying to avoid
              of?). Look at the following article
              >
              How to locate and replace special characters in an XML file with
              Visual C# .NEThttp://support.microso ft.com/kb/316063
              >
              Hope this helps
              >
              Hi,
              >
              Thanks again for the prompt reply.  What I'm basically after is that
              if someone makes a change to the XML in the textbox, I want to verify
              that it is well formed XML before saving it into the database.  If
              it's not well formed then I can display a message box, which I have
              already written, explaining to the user that it's not valid XML.  They
              can then make the relevant change before saving it.
              >
              Hope that makes sense.
              >
              Really appreciate your help.- Hide quoted text -
              >
              - Show quoted text -
              >
              If you would edit it in the textbox, then you can simply use the
              XmlTextReader:http://msdn.microsoft.com/en-us/libr...extreader.aspx
              >
              XmlReader r = new XmlTextReader(n ew StringReader(Te xtBox1.Text));
              >
              try {
              while (r.Read())
              {
              //XML is well-formed
              >
              } catch (Exception e) {
              //error
              }
              >
              And if XML is not syntactically correct, the XML parser will raise an
              error.
              Thank you very much; that's exactly what I'm after.

              Comment

              Working...