Parse XML

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

    Parse XML

    Im new to c#
    what's the easiest way to parse short xml files?

    <html><head><ti tle>Title of the
    page</title><script></script></head><body>BODY </body></html>

    and save it to an object for access later?

    Class HtmlFile
    {
    public string Title
    public string SciptType
    public string SciptSrc
    public string Body
    }

    XmlNodeReader seems very confusing

    Sami

  • zacks@construction-imaging.com

    #2
    Re: Parse XML

    On Aug 5, 3:46 pm, "Sami" <sam...@ymail.c omwrote:
    Im new to c#
    what's the easiest way to parse short xml files?
    >
    <html><head><ti tle>Title of the
    page</title><script></script></head><body>BODY </body></html>
    >
    and save it to an object for access later?
    >
    Class HtmlFile
    {
    public string Title
    public string SciptType
    public string SciptSrc
    public string Body
    >
    }
    >
    XmlNodeReader seems very confusing
    >
    The two basic methods of loading an XML file from my experiance is 1)
    use the DOM (Document Object Model) which uses the XmlDocument object
    to load the file, and the SelectSingleNod e method to get the value of
    a single node, and 2) use the XmlSerializer class.

    Comment

    • Sami

      #3
      Re: Parse XML

      if it's not too much trouble could someone provide a sample on how to do
      this?

      thank you
      Sami
      <zacks@construc tion-imaging.comwrot e in message
      news:02ef3fa0-e9cd-49a9-8c76-5d21a4a6434d@26 g2000hsk.google groups.com...
      On Aug 5, 3:46 pm, "Sami" <sam...@ymail.c omwrote:
      Im new to c#
      what's the easiest way to parse short xml files?
      >
      <html><head><ti tle>Title of the
      page</title><script></script></head><body>BODY </body></html>
      >
      and save it to an object for access later?
      >
      Class HtmlFile
      {
      public string Title
      public string SciptType
      public string SciptSrc
      public string Body
      >
      }
      >
      XmlNodeReader seems very confusing
      >
      The two basic methods of loading an XML file from my experiance is 1)
      use the DOM (Document Object Model) which uses the XmlDocument object
      to load the file, and the SelectSingleNod e method to get the value of
      a single node, and 2) use the XmlSerializer class.

      Comment

      • xpista

        #4
        Re: Parse XML

        if it's not too much trouble could someone provide a sample on how to do
        this?
        Using Linq to Xml:

        static void Main(string[] args) {
        string xml = "<html><head><t itle>Title of the page</
        title><script></script></head><body>BODY </body></html>";
        HtmlFile hf = new HtmlFile(xml);
        }

        class HtmlFile {
        public string Title;
        public string Script;
        public string Body;
        // ...

        public HtmlFile(string xml) {
        XElement e = XElement.Parse( xml);

        if (e.Element("hea d") != null) {
        Title =
        (string)e.Eleme nt("head").Elem ent("title");
        Script =
        (string)e.Eleme nt("head").Elem ent("script");
        }
        Body = (string)e.Eleme nt("body");
        // ...
        }

        Comment

        Working...