interacting with xml

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

    interacting with xml


    hi guys , I'm mew to system.xml my question is :
    what is the right way to read / write value to the "First_Name " node ?
    and same question : how do I read/ write to the category property (inside
    the CD node) ?

    thank you very much
    Sharon

    <Catalog>
    <Cd category="rock" >
    <Title>Empire Burlesque</Title>
    <Artist>
    <First_Name>Bob </First_Name>
    <Last_Name>Dyla n</Last_Name>
    </Artist>
    <Country>Usa</Country>
    <Company>Columb ia</Company>
    <Price>10.90</Price>
    <Year>1985</Year>
    </Cd>
    <Cd category="pop">
    <Title>Hide Your Heart</Title>
    <Artist>
    <First_Name>Bon nie</First_Name>
    <Last_Name>Tyle r</Last_Name>
    </Artist>
    <Country>Uk</Country>
    <Company>Cbs Records</Company>
    <Price>9.90</Price>
    <Year>1988</Year>
    </Cd>


  • Angel J. Hernández M.

    #2
    Re: interacting with xml

    Hi there... you can do it like this...

    System.Xml.XmlD ocument doc = new System.Xml.XmlD ocument(); // Used to load
    the xml document
    System.Xml.XmlN ode node1, node2; // Used to store information about those
    nodes you're interested in
    string node1_value, node2_value; // Used to store and read the values from
    the nodes
    doc.Load(@"c:\s ample.xml"); // Loading of xml document
    node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist"); //
    Select the node by using a XPath query
    node1_value = node1["First_Name "].InnerText; // Store the value of the first
    node
    node1["First_Name "].InnerXml = "Angel"; // Change the value... I wrote my
    name ;-)
    node2 = doc.SelectSingl eNode("Catalog/Cd[@category='pop']"); // Select the
    node once again by using XPath
    node2_value = node2.Attribute s["category"].InnerText; // Get the value for
    the attribute
    node2.Attribute s["category"].InnerXml="jazz "; // Set the new value for the
    attribute
    doc.Save(@"c:\m odified.xml"); // Save the modified document

    and that's it! The catalog's closing tag is missing in the xml you send so
    you need to write it first in order to test the code.
    Hope this may help you... Have a nice day! :D

    Regards,


    --
    Angel J. Hernández M.
    MCP - MCAD - MCSD - MCDBA

    ¿Sabes cómo proteger tu empresa de un ciberataque? En Consein te guiamos paso a paso con estrategias efectivas para una defensa sólida. Protégete ahora Únicos en Venezuela:Expertos en Migración de Infraestructura y Bases de Datos a Microsoft Azure Ofrecemos a nuestros clientes migración segura y eficiente a Azure, optimización de costos, mejor rendimiento, tecnologías avanzadas, […]



    "Sharon" <Sharon669@hotm ail.com> escribió en el mensaje
    news:OmKdhS1YFH A.2508@TK2MSFTN GP15.phx.gbl...[color=blue]
    >
    > hi guys , I'm mew to system.xml my question is :
    > what is the right way to read / write value to the "First_Name " node ?
    > and same question : how do I read/ write to the category property (inside
    > the CD node) ?
    >
    > thank you very much
    > Sharon
    >
    > <Catalog>
    > <Cd category="rock" >
    > <Title>Empire Burlesque</Title>
    > <Artist>
    > <First_Name>Bob </First_Name>
    > <Last_Name>Dyla n</Last_Name>
    > </Artist>
    > <Country>Usa</Country>
    > <Company>Columb ia</Company>
    > <Price>10.90</Price>
    > <Year>1985</Year>
    > </Cd>
    > <Cd category="pop">
    > <Title>Hide Your Heart</Title>
    > <Artist>
    > <First_Name>Bon nie</First_Name>
    > <Last_Name>Tyle r</Last_Name>
    > </Artist>
    > <Country>Uk</Country>
    > <Company>Cbs Records</Company>
    > <Price>9.90</Price>
    > <Year>1988</Year>
    > </Cd>
    >[/color]


    Comment

    • Sharon

      #3
      Re: interacting with xml

      Thank you Angel

      After changing from this line:
      node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist");
      to this :

      node1 = doc.SelectSingl eNode("Catalog/Cd[@category=\"roc k\"]/Artist");

      It’s working great!!!

      Thank you very much

      Sharon



      Angel J. Hernández M." <angeljesus14@h otmail.com> wrote in message
      news:eHqDK25YFH A.4088@TK2MSFTN GP15.phx.gbl...
      [color=blue]
      > Hi there... you can do it like this...
      >
      > System.Xml.XmlD ocument doc = new System.Xml.XmlD ocument(); // Used to load
      > the xml document
      > System.Xml.XmlN ode node1, node2; // Used to store information about those
      > nodes you're interested in
      > string node1_value, node2_value; // Used to store and read the values from
      > the nodes
      > doc.Load(@"c:\s ample.xml"); // Loading of xml document
      > node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist"); //
      > Select the node by using a XPath query
      > node1_value = node1["First_Name "].InnerText; // Store the value of the
      > first node
      > node1["First_Name "].InnerXml = "Angel"; // Change the value... I wrote my
      > name ;-)
      > node2 = doc.SelectSingl eNode("Catalog/Cd[@category='pop']"); // Select the
      > node once again by using XPath
      > node2_value = node2.Attribute s["category"].InnerText; // Get the value for
      > the attribute
      > node2.Attribute s["category"].InnerXml="jazz "; // Set the new value for the
      > attribute
      > doc.Save(@"c:\m odified.xml"); // Save the modified document
      >
      > and that's it! The catalog's closing tag is missing in the xml you send so
      > you need to write it first in order to test the code.
      > Hope this may help you... Have a nice day! :D
      >
      > Regards,
      >
      >
      > --
      > Angel J. Hernández M.
      > MCP - MCAD - MCSD - MCDBA
      > http://groups.msn.com/desarrolladoresmiranda
      > http://www.consein.com
      >
      >
      > "Sharon" <Sharon669@hotm ail.com> escribió en el mensaje
      > news:OmKdhS1YFH A.2508@TK2MSFTN GP15.phx.gbl...[color=green]
      >>
      >> hi guys , I'm mew to system.xml my question is :
      >> what is the right way to read / write value to the "First_Name " node ?
      >> and same question : how do I read/ write to the category property (inside
      >> the CD node) ?
      >>
      >> thank you very much
      >> Sharon
      >>
      >> <Catalog>
      >> <Cd category="rock" >
      >> <Title>Empire Burlesque</Title>
      >> <Artist>
      >> <First_Name>Bob </First_Name>
      >> <Last_Name>Dyla n</Last_Name>
      >> </Artist>
      >> <Country>Usa</Country>
      >> <Company>Columb ia</Company>
      >> <Price>10.90</Price>
      >> <Year>1985</Year>
      >> </Cd>
      >> <Cd category="pop">
      >> <Title>Hide Your Heart</Title>
      >> <Artist>
      >> <First_Name>Bon nie</First_Name>
      >> <Last_Name>Tyle r</Last_Name>
      >> </Artist>
      >> <Country>Uk</Country>
      >> <Company>Cbs Records</Company>
      >> <Price>9.90</Price>
      >> <Year>1988</Year>
      >> </Cd>
      >>[/color]
      >
      >[/color]


      Comment

      • Angel J. Hernández M.

        #4
        Re: interacting with xml

        You're welcome!!! :D Happy Programming

        Regards,


        --
        Angel J. Hernández M.
        MCP - MCAD - MCSD - MCDBA

        ¿Sabes cómo proteger tu empresa de un ciberataque? En Consein te guiamos paso a paso con estrategias efectivas para una defensa sólida. Protégete ahora Únicos en Venezuela:Expertos en Migración de Infraestructura y Bases de Datos a Microsoft Azure Ofrecemos a nuestros clientes migración segura y eficiente a Azure, optimización de costos, mejor rendimiento, tecnologías avanzadas, […]



        "Sharon" <Sharon669@hotm ail.com> escribió en el mensaje
        news:%23i4wSK9Y FHA.2508@TK2MSF TNGP15.phx.gbl. ..[color=blue]
        > Thank you Angel
        >
        > After changing from this line:
        > node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist");
        > to this :
        >
        > node1 = doc.SelectSingl eNode("Catalog/Cd[@category=\"roc k\"]/Artist");
        >
        > It's working great!!!
        >
        > Thank you very much
        >
        > Sharon
        >
        >
        >
        > Angel J. Hernández M." <angeljesus14@h otmail.com> wrote in message
        > news:eHqDK25YFH A.4088@TK2MSFTN GP15.phx.gbl...
        >[color=green]
        >> Hi there... you can do it like this...
        >>
        >> System.Xml.XmlD ocument doc = new System.Xml.XmlD ocument(); // Used to
        >> load the xml document
        >> System.Xml.XmlN ode node1, node2; // Used to store information about those
        >> nodes you're interested in
        >> string node1_value, node2_value; // Used to store and read the values
        >> from the nodes
        >> doc.Load(@"c:\s ample.xml"); // Loading of xml document
        >> node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist"); //
        >> Select the node by using a XPath query
        >> node1_value = node1["First_Name "].InnerText; // Store the value of the
        >> first node
        >> node1["First_Name "].InnerXml = "Angel"; // Change the value... I wrote my
        >> name ;-)
        >> node2 = doc.SelectSingl eNode("Catalog/Cd[@category='pop']"); // Select
        >> the node once again by using XPath
        >> node2_value = node2.Attribute s["category"].InnerText; // Get the value
        >> for the attribute
        >> node2.Attribute s["category"].InnerXml="jazz "; // Set the new value for
        >> the attribute
        >> doc.Save(@"c:\m odified.xml"); // Save the modified document
        >>
        >> and that's it! The catalog's closing tag is missing in the xml you send
        >> so you need to write it first in order to test the code.
        >> Hope this may help you... Have a nice day! :D
        >>
        >> Regards,
        >>
        >>
        >> --
        >> Angel J. Hernández M.
        >> MCP - MCAD - MCSD - MCDBA
        >> http://groups.msn.com/desarrolladoresmiranda
        >> http://www.consein.com
        >>
        >>
        >> "Sharon" <Sharon669@hotm ail.com> escribió en el mensaje
        >> news:OmKdhS1YFH A.2508@TK2MSFTN GP15.phx.gbl...[color=darkred]
        >>>
        >>> hi guys , I'm mew to system.xml my question is :
        >>> what is the right way to read / write value to the "First_Name " node ?
        >>> and same question : how do I read/ write to the category property
        >>> (inside the CD node) ?
        >>>
        >>> thank you very much
        >>> Sharon
        >>>
        >>> <Catalog>
        >>> <Cd category="rock" >
        >>> <Title>Empire Burlesque</Title>
        >>> <Artist>
        >>> <First_Name>Bob </First_Name>
        >>> <Last_Name>Dyla n</Last_Name>
        >>> </Artist>
        >>> <Country>Usa</Country>
        >>> <Company>Columb ia</Company>
        >>> <Price>10.90</Price>
        >>> <Year>1985</Year>
        >>> </Cd>
        >>> <Cd category="pop">
        >>> <Title>Hide Your Heart</Title>
        >>> <Artist>
        >>> <First_Name>Bon nie</First_Name>
        >>> <Last_Name>Tyle r</Last_Name>
        >>> </Artist>
        >>> <Country>Uk</Country>
        >>> <Company>Cbs Records</Company>
        >>> <Price>9.90</Price>
        >>> <Year>1988</Year>
        >>> </Cd>
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: interacting with xml

          Sharon <Sharon669@hotm ail.com> wrote:[color=blue]
          > Thank you Angel
          >
          > After changing from this line:
          > node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist");
          > to this :
          >
          > node1 = doc.SelectSingl eNode("Catalog/Cd[@category=\"roc k\"]/Artist");
          >
          > It=3Fs working great!!![/color]

          Hmm... I don't think you should have needed to do that (and if you
          don't have to, then obviously the first is more readable :)

          Could you post a short but complete program which shows the difference
          between them.

          See http://www.pobox.com/~skeet/csharp/complete.html for details of
          what I mean by that.

          Here's an example showing them giving the same answer:

          using System;
          using System.Xml;

          class Test
          {
          static void Main()
          {
          string xml = @"<?xml version=""1.0"" ?>
          <Catalog>
          <Cd category=""rock "">
          <Title>Empire Burlesque</Title>
          <Artist>
          <First_Name>Bob </First_Name>
          <Last_Name>Dyla n</Last_Name>
          </Artist>
          <Country>Usa</Country>
          <Company>Columb ia</Company>
          <Price>10.90</Price>
          <Year>1985</Year>
          </Cd>
          <Cd category=""pop" ">
          <Title>Hide Your Heart</Title>
          <Artist>
          <First_Name>Bon nie</First_Name>
          <Last_Name>Tyle r</Last_Name>
          </Artist>
          <Country>Uk</Country>
          <Company>Cbs Records</Company>
          <Price>9.90</Price>
          <Year>1988</Year>
          </Cd>
          </Catalog>";

          XmlDocument doc = new XmlDocument();
          doc.LoadXml(xml );

          XmlNode node1 = doc.SelectSingl eNode
          ("Catalog/Cd[@category=\"roc k\"]/Artist");
          XmlNode node2 = doc.SelectSingl eNode
          ("Catalog/Cd[@category='rock ']/Artist");

          Console.WriteLi ne (node1==node2);
          }
          }

          It prints out "True" showing that the results are the same for both
          calls.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Sharon

            #6
            Re: interacting with xml

            My env is

            Xp pro

            Net 2003 VER 7.1.3088

            Microsoft .NET Framework SDK v1.1



            But I tested the code today and it's working fine (both versions :)



            v1 : node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist");


            and this :


            v2 : node1 = doc.SelectSingl eNode("Catalog/Cd[@category=\"roc k\"]/Artist");





            I don't know how is it possible but v1 returned null yesterday
            :( and I swear didn't touch the code



            Sorry

            Sharon





            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1d0364 46d4691f4798c20 e@msnews.micros oft.com...[color=blue]
            > Sharon <Sharon669@hotm ail.com> wrote:[color=green]
            >> Thank you Angel
            >>
            >> After changing from this line:
            >> node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist");
            >> to this :
            >>
            >> node1 =
            >> doc.SelectSingl eNode("Catalog/Cd[@category=\"roc k\"]/Artist");
            >>
            >> It=3Fs working great!!![/color]
            >
            > Hmm... I don't think you should have needed to do that (and if you
            > don't have to, then obviously the first is more readable :)
            >
            > Could you post a short but complete program which shows the difference
            > between them.
            >
            > See http://www.pobox.com/~skeet/csharp/complete.html for details of
            > what I mean by that.
            >
            > Here's an example showing them giving the same answer:
            >
            > using System;
            > using System.Xml;
            >
            > class Test
            > {
            > static void Main()
            > {
            > string xml = @"<?xml version=""1.0"" ?>
            > <Catalog>
            > <Cd category=""rock "">
            > <Title>Empire Burlesque</Title>
            > <Artist>
            > <First_Name>Bob </First_Name>
            > <Last_Name>Dyla n</Last_Name>
            > </Artist>
            > <Country>Usa</Country>
            > <Company>Columb ia</Company>
            > <Price>10.90</Price>
            > <Year>1985</Year>
            > </Cd>
            > <Cd category=""pop" ">
            > <Title>Hide Your Heart</Title>
            > <Artist>
            > <First_Name>Bon nie</First_Name>
            > <Last_Name>Tyle r</Last_Name>
            > </Artist>
            > <Country>Uk</Country>
            > <Company>Cbs Records</Company>
            > <Price>9.90</Price>
            > <Year>1988</Year>
            > </Cd>
            > </Catalog>";
            >
            > XmlDocument doc = new XmlDocument();
            > doc.LoadXml(xml );
            >
            > XmlNode node1 = doc.SelectSingl eNode
            > ("Catalog/Cd[@category=\"roc k\"]/Artist");
            > XmlNode node2 = doc.SelectSingl eNode
            > ("Catalog/Cd[@category='rock ']/Artist");
            >
            > Console.WriteLi ne (node1==node2);
            > }
            > }
            >
            > It prints out "True" showing that the results are the same for both
            > calls.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too[/color]


            Comment

            • clintonG

              #7
              Re: interacting with xml

              You can also learn something from Dan Whalin's videos [1]

              <%= Clinton Gallagher
              METROmilwaukee (sm) "A Regional Information Service"
              NET csgallagher AT metromilwaukee. com
              URL http://metromilwaukee.com/
              URL http://clintongallagher.metromilwaukee.com/

              [1] http://xmlforasp.net/

              "Sharon" <Sharon669@hotm ail.com> wrote in message
              news:OSbrwKIZFH A.3184@TK2MSFTN GP15.phx.gbl...[color=blue]
              > My env is
              >
              > Xp pro
              >
              > Net 2003 VER 7.1.3088
              >
              > Microsoft .NET Framework SDK v1.1
              >
              >
              >
              > But I tested the code today and it's working fine (both versions :)
              >
              >
              >
              > v1 : node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist");
              >
              >
              > and this :
              >
              >
              > v2 : node1 =
              > doc.SelectSingl eNode("Catalog/Cd[@category=\"roc k\"]/Artist");
              >
              >
              >
              >
              >
              > I don't know how is it possible but v1 returned null yesterday
              > :( and I swear didn't touch the code
              >
              >
              >
              > Sorry
              >
              > Sharon
              >
              >
              >
              >
              >
              > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              > news:MPG.1d0364 46d4691f4798c20 e@msnews.micros oft.com...[color=green]
              >> Sharon <Sharon669@hotm ail.com> wrote:[color=darkred]
              >>> Thank you Angel
              >>>
              >>> After changing from this line:
              >>> node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist");
              >>> to this :
              >>>
              >>> node1 =
              >>> doc.SelectSingl eNode("Catalog/Cd[@category=\"roc k\"]/Artist");
              >>>
              >>> It=3Fs working great!!![/color]
              >>
              >> Hmm... I don't think you should have needed to do that (and if you
              >> don't have to, then obviously the first is more readable :)
              >>
              >> Could you post a short but complete program which shows the difference
              >> between them.
              >>
              >> See http://www.pobox.com/~skeet/csharp/complete.html for details of
              >> what I mean by that.
              >>
              >> Here's an example showing them giving the same answer:
              >>
              >> using System;
              >> using System.Xml;
              >>
              >> class Test
              >> {
              >> static void Main()
              >> {
              >> string xml = @"<?xml version=""1.0"" ?>
              >> <Catalog>
              >> <Cd category=""rock "">
              >> <Title>Empire Burlesque</Title>
              >> <Artist>
              >> <First_Name>Bob </First_Name>
              >> <Last_Name>Dyla n</Last_Name>
              >> </Artist>
              >> <Country>Usa</Country>
              >> <Company>Columb ia</Company>
              >> <Price>10.90</Price>
              >> <Year>1985</Year>
              >> </Cd>
              >> <Cd category=""pop" ">
              >> <Title>Hide Your Heart</Title>
              >> <Artist>
              >> <First_Name>Bon nie</First_Name>
              >> <Last_Name>Tyle r</Last_Name>
              >> </Artist>
              >> <Country>Uk</Country>
              >> <Company>Cbs Records</Company>
              >> <Price>9.90</Price>
              >> <Year>1988</Year>
              >> </Cd>
              >> </Catalog>";
              >>
              >> XmlDocument doc = new XmlDocument();
              >> doc.LoadXml(xml );
              >>
              >> XmlNode node1 = doc.SelectSingl eNode
              >> ("Catalog/Cd[@category=\"roc k\"]/Artist");
              >> XmlNode node2 = doc.SelectSingl eNode
              >> ("Catalog/Cd[@category='rock ']/Artist");
              >>
              >> Console.WriteLi ne (node1==node2);
              >> }
              >> }
              >>
              >> It prints out "True" showing that the results are the same for both
              >> calls.
              >>
              >> --
              >> Jon Skeet - <skeet@pobox.co m>
              >> http://www.pobox.com/~skeet
              >> If replying to the group, please do not mail me too[/color]
              >
              >[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: interacting with xml

                Sharon <Sharon669@hotm ail.com> wrote:[color=blue]
                > But I tested the code today and it's working fine (both versions :)
                > v1 : node1 = doc.SelectSingl eNode("Catalog/Cd[@category='rock ']/Artist");
                > and this :
                > v2 : node1 = doc.SelectSingl eNode("Catalog/Cd[@category=\"roc k\"]/Artist");
                > I don't know how is it possible but v1 returned null yesterday
                > :( and I swear didn't touch the code[/color]

                I suspect you touched *something* - it's just a case of working out
                what :)

                As I say, I'd go with the first version if I were you - it's a bit
                easier to read!

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                Working...