locked
Please Suggest me... RRS feed

  • Question

  • I want to take a value from a TextBox and that I want to retrieve nodes based on this value. I  want to retrieve the complete element having an attribute value.

     

    e.g. 

    <?xml version="1.0"?>

    <info>

           <book id="b1">

                  <name nm="c++"  lang="eng"  />

                  <price>250</price>

          </book>

           <book id="b2">

                  <name nm="c#"  lang="eng"  />

                  <price>560</price>

          </book>

    </info> 

     

    My Question is to retrieve the attribute values according to my wish, if in a textbox if i give the value

    i.e TextBox.Text="b1" then i should retrieve  nm="c++", lang="eng".

     

     

    With KindRegards

     

    Tapan

    Tuesday, April 17, 2007 10:41 AM

Answers

  • Hi,

    The following code snippet may help you,

     

    Code Snippet

     

                XmlDocument doc = new XmlDocument();

                doc.Load("bookinfo.xml");

                XmlNode node = doc.SelectSingleNode("//book[@id='" + TextBox1.Text.Trim()  + "']/name");

                if (node != null)

                {

                    string nm = node.Attributes["nm"].InnerText;

                    string lang = node.Attributes["lang"].InnerText;

                }

                else

                {

                    MessageBox.Shw("No such book element found having id = {0}", TextBox1.Text);

                }

     

    HTH,
    Thursday, April 19, 2007 6:45 AM
  • Hi, 

               

    Code Snippet

    XmlDocument doc = new XmlDocument();

    doc.Load("bookinfo.xml");

    XmlNode node = doc.SelectSingleNode("//book[@id='" + TextBox1.Text.Trim()  + "']/name");

    if (node != null)

    {

        node.Attributes["nm"].InnerText = TextBox2.Text;

        node.Attributes["lang"].InnerText = TextBox3.Text;

    }

    doc.Save("bookinfo.xml");

     

     

    NOTE: Please use new thread to post your new question and mark EACH post as ANSWER which really gives you answer so that who have same problem; can find the answer easily

     

     HTH,

    Friday, May 4, 2007 6:03 PM

All replies

  • Tapan,

    You can use an XPath expression to retrieve attribute values. Here is a sample which retrieves attributes from name element where id attribute in book element is set to 'b1'

    Code Snippet

    //info/book[@id='b1']/name/@*


    I also noticed that your xml is not wellformed.
    Tuesday, April 17, 2007 11:27 AM
  • Hi,

    The following code snippet may help you,

     

    Code Snippet

     

                XmlDocument doc = new XmlDocument();

                doc.Load("bookinfo.xml");

                XmlNode node = doc.SelectSingleNode("//book[@id='" + TextBox1.Text.Trim()  + "']/name");

                if (node != null)

                {

                    string nm = node.Attributes["nm"].InnerText;

                    string lang = node.Attributes["lang"].InnerText;

                }

                else

                {

                    MessageBox.Shw("No such book element found having id = {0}", TextBox1.Text);

                }

     

    HTH,
    Thursday, April 19, 2007 6:45 AM
  •  Thanks   it  works.....

     

    Now i am giving another xml doc.

    <?xml version="1.0" ?>

    - <info>
    -        <book id="b1">
                     <name nm="c++" lang="eng" />
                     <name nm="c#" lang="frn" />
         </book>
    -      <book id="b2">
                <name nm="vb" lang="eng" />
                <name nm="j#" lang="ory" />
        </book>
    </info>
     
    Now  if  i  give   in a Textbox1 the  attribute value of  id="b1"
     
    then this should access the attribute value as  in 
     
     textbox2="eng"   from  <name nm="c++"   lang="eng" />    and
    textbox3="frn"    from  <name nm="c#"       lang="frn" />
     
    With Regards
    Tapan
    Friday, April 27, 2007 5:56 AM
  • Hi,

    You can do this easily using the previously posted example code

    Code Snippet

     

    XmlDocument doc = new XmlDocument();

    doc.Load("bookinfo.xml");

    XmlNode node = doc.SelectSingleNode("//book[@id='" + TextBox1.Text.Trim() + "']");

    if (node != null)

    {

         TextBox2.Text = node.ChildNodes[0].Attributes["lang"].InnerText;

         TextBox3.Text = node.ChildNodes[1].Attributes["lang"].InnerText;

    }

    else

    {

         MessageBox.Show("No such book element found having id = {0}", TextBox1.Text);

    }

     

    HTH,
    Friday, April 27, 2007 5:46 PM
  • Thanks the above code works properly, suppose my xml file is as follows....

     

    <?xml version="1.0" ?>

    - <info>
    -        <book id="b1">
                     <name nm="c++" lang="eng" data="c:\data\file.fdb" />
                     <name nm="c#" lang="frn" data="d:\data\file1.fdb" />
         </book>
    -      <book id="b2">
                <name nm="vb" lang="eng"  data="c:\data\file2.fdb" />
                <name nm="j#" lang="ory"   data= "d:\data\file3.fdb" />
        </book>
    </info>
     
    ok, now i want to retrieve the attribute value from data="c:\data\file.fdb"  to the following database connection string in c#.
     

    FbConnection cn = new FbConnection("userid=SYSDBA;password=masterkey;database ="here i want to access  the attribute value of data " );  

     

    i.e.

     

    FbConnection cn = new FbConnection("userid=SYSDBA;password=masterkey;database  = "c:\data\file.fdb" );

     

    With regards

     

    Tapan

    Thursday, May 3, 2007 6:28 AM
  • Hi if i want to fetch the value of <price> which doesn have any attribute can i use XPath.
    Thursday, May 3, 2007 7:16 AM
  •  Tapan wrote:

    <info>

    -        <book id="b1">
                     <name nm="c++" lang="eng" data="c:\data\file.fdb" />
                     <name nm="c#" lang="frn" data="d:\data\file1.fdb" />
         </< FONT>book>
    -      <book id="b2">
                <name nm="vb" lang="eng"  data="c:\data\file2.fdb" />
                <name nm="j#" lang="ory"   data= "d:\data\file3.fdb" />
        </< FONT>book>
    </< FONT>info>
     
    ok, now i want to retrieve the attribute value from data="c:\data\file.fdb"  to the following database connection string in c#.
     

     

    For the given XML above, the following XPath will do the job for you

    Code Snippet

    XmlDocument doc = new XmlDocument();

    doc.Load("Test1.xml");

    XmlNode node = doc.SelectSingleNode("//name[@nm='c#']/@data");

    if (node != null)

    {

    string data = node.InnerText;

    }

    else

    {

    MessageBox.Show("No such element found");

    }

     

    Please mark best and acceptable ALL replie(s) as the ANSWER(S)

     

    HTH,

    Thursday, May 3, 2007 10:10 AM
  •  Niranjan_007 wrote:
    Hi if i want to fetch the value of which doesn have any attribute can i use XPath.

     

    Hi,

    Yes, you can do this. XML apis works almost same way for Nodes and Attrributes or you can say all type of elements

    e.g.

    Code Snippet

    XmlNode node = doc.SelectSingleNode("//price");

    string valuefrompriceelement = node.InnerText;

     

    HTH,
    Thursday, May 3, 2007 10:16 AM
  • Please suggest me that how can i update my xml file just by inserting new data....

    My xml file is here:

     

               

    <?xml version="1.0"?>

    <info>

           <book id="b1">

                  <name nm="c++"  lang="eng"  />

                  <price>250</price>

          </book>

           <book id="b2">

                  <name nm="c#"  lang="eng"  />

                  <price>560</price>

          </book>

    </info> 

    Now  My requirement is that, I want to  update my xml file by select  the attribute value of id="b1" in a TextBox.

    According to taht, when I insert the value for the nm="java" instead of nm="c++" or lang="ori" instead of lang="eng"

    using two textBoxes..., then my xml should update according to the new values  for id="b1" as..

     

    <?xml version="1.0"?>

    <info>

           <book id="b1">

                  <name nm="java" lang="ori"  />

                  <price>250</price>

          </book>

           <book id="b2">

                  <name nm="c#"  lang="eng"  />

                  <price>560</price>

          </book>

    </info> 

     

    NOTE : there should not be any duplicate value in my xml

    i.e.

    <?xml version="1.0"?>

    <info>

           <book id="b1">

                  <name nm="c++"  lang="eng"  />

                  <price>250</price>

          </book>

    <book id="b1">

                  <name nm="java" lang="ori"  />

                  <price>250</price>

          </book>

     

           <book id="b2">

                  <name nm="c#"  lang="eng"  />

                  <price>560</price>

          </book>

    </info> 

     

     

    Friday, May 4, 2007 2:19 PM
  • Hi, 

               

    Code Snippet

    XmlDocument doc = new XmlDocument();

    doc.Load("bookinfo.xml");

    XmlNode node = doc.SelectSingleNode("//book[@id='" + TextBox1.Text.Trim()  + "']/name");

    if (node != null)

    {

        node.Attributes["nm"].InnerText = TextBox2.Text;

        node.Attributes["lang"].InnerText = TextBox3.Text;

    }

    doc.Save("bookinfo.xml");

     

     

    NOTE: Please use new thread to post your new question and mark EACH post as ANSWER which really gives you answer so that who have same problem; can find the answer easily

     

     HTH,

    Friday, May 4, 2007 6:03 PM
  • in my code  doc.Load("bookinfo.xml"); 

    How can i load my xml file using relative path; i.e suppose i have a folder named as test;

    Inside that folder, i have two another subfolders, those are xml & bin.

    I want to install my setup program within that bin folder, Already my bookifo.xml has kept under xml folder.

     

    Now my objective is to how can i retrieve(how can i use the relative path) in the above                     doc.Load("bookinfo.xml");so, that it can find the xml file  from xml folder & run smoothly.

     

    With regards

     

    Tapan

    Monday, May 7, 2007 5:02 AM
  •  

    Please help me using XSLT2. I am giving the code which shows error ...

     

          Altova.AltovaXML.XSLT2  app = new   Altova.AltovaXML.XSLT2 ();   //The type  Altova.AltovaXML.XSLT2 class has no constructor defined
                app.XSLFileName = "C:\\exchange\\cdr-conversion\\cdr-conversion\\input\\abc12.XSL";
                app.InputXMLFileName = "C:\\exchange\\cdr-conversion\\cdr-conversion\\input\\output222.xml";
                app.Execute("C:\\exchange\\cdr-conversion\\cdr-conversion\\input\\outputx.html"

    Friday, July 20, 2007 11:42 AM