access programmatically XML element change value, then put the xml string in a string variable

Answered access programmatically XML element change value, then put the xml string in a string variable

  • Thursday, April 12, 2012 10:25 AM
     
     

    Hi

    I have one xml file which i would like to access programmatically and then access two elements change the values of it(BrandName, Manufacturer) and then put the resultant xml after the change to a string variable in c#

    <?xml version="1.0" encoding="utf-8"?>
     
    <Product ID="001" Name="Soap"> 
     
     
    <Price>10.00</Price> 
     
     
    <OtherDetails> 
     
       
    <BrandName>PlaceHolder1</BrandName> 
     
       
    <Manufacturer>PlaceHolder2</Manufacturer> 
     
     
    </OtherDetails> 
     
    </Product>

All Replies

  • Thursday, April 12, 2012 10:42 AM
     
     Proposed Has Code

    Here is a sample you can use with .NET 3.5 or later:

                XDocument doc = XDocument.Load("input.xml");
                doc.Root.Element("OtherDetails").SetElementValue("BrandName", "foo");
                doc.Root.Element("OtherDetails").SetElementValue("Manufacturer", "bar");
                string xml = doc.ToString();

    Read more about LINQ to XML .


    MVP Data Platform Development My blog

    • Proposed As Answer by Martin Honnen Thursday, April 12, 2012 11:05 AM
    •  
  • Friday, April 13, 2012 6:21 AM
     
     

    Thanks for the reply

    However i am getting object reference not set to an instance exception. if there is  amchange in the xml say

    <?xml version="1.0" encoding="utf-8"?>
     
    <Order>
    <Product ID="001" Name="Soap"> 
     
     
    <Price>10.00</Price> 
     
     
    <ns:OtherDetails xmlns:ns="http://abc.com/ab"> 
     
       
    <BrandName>PlaceHolder1</BrandName> 
     
       
    <Manufacturer>PlaceHolder2</Manufacturer> 
     
     
    </OtherDetails> 
     
    </Product>

    </Order>

  • Friday, April 13, 2012 9:29 AM
     
      Has Code

    I suggest you start reading the LINQ to XML documentation I pointed you to in my earlier answer.  As for your latest XML sample (assuming it is well-formed after changing </OtherDetails> into </ns:OtherDetails>), it has an element in a namespace so you need to adapt the code to

                XDocument doc = XDocument.Load("input.xml");
                XNamespace ns = "http://abc.com/ab";
                XElement details = doc.Root.Element("Product").Element(ns + "OtherDetails");
                details.SetElementValue("BrandName", "foo");
                details.SetElementValue("Manufacturer", "bar");
                string xml = doc.ToString();


    MVP Data Platform Development My blog

  • Friday, April 13, 2012 1:23 PM
     
      Has Code

    i am getting object reference not set to an instance exception.

    and i get it on

    Element(ns + "OtherDetails");

  • Saturday, April 14, 2012 9:50 AM
     
     Answered Has Code

    i am getting object reference not set to an instance exception.

    and i get it on

    Element(ns + "OtherDetails");

    Well the posted sample was not even well-formed, after correcting it to

    <?xml version="1.0" encoding="utf-8"?>
    <Order>
      <Product ID="001" Name="Soap">
    
        <Price>10.00</Price>
    
        <ns:OtherDetails xmlns:ns="http://abc.com/ab">
    
          <BrandName>PlaceHolder1</BrandName>
    
          <Manufacturer>PlaceHolder2</Manufacturer>
    
          </ns:OtherDetails>
    
        </Product>
    
    </Order>

    I tested the code

                XDocument doc = XDocument.Load("input.xml");
                XNamespace ns = "http://abc.com/ab";
                XElement details = doc.Root.Element("Product").Element(ns + "OtherDetails");
                details.SetElementValue("BrandName", "foo");
                details.SetElementValue("Manufacturer", "bar");
                string xml = doc.ToString();
                Console.WriteLine(xml);

    it compiles and runs fine for me, outputting

    <Order>
      <Product ID="001" Name="Soap">
        <Price>10.00</Price>
        <ns:OtherDetails xmlns:ns="http://abc.com/ab">
          <BrandName>foo</BrandName>
          <Manufacturer>bar</Manufacturer>
        </ns:OtherDetails>
      </Product>
    </Order>

    I will repeat my suggestion to start reading and working through LINQ to XML , if you then still have problems you will need to show us minimal but complete enough samples allowing us to reproduce the error.


    MVP Data Platform Development My blog

    • Marked As Answer by NabX Saturday, April 14, 2012 7:08 PM
    •