Click here to Skip to main content
15,915,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys i would like to ask how will i edit 1 specific content in my xml file using csharp? assuming i have an xml file that has this value

XML
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="CityId" type="xs:int" minOccurs="0" />
                <xs:element name="CountryId" type="xs:int" minOccurs="0" />
                <xs:element name="RegionId" type="xs:int" minOccurs="0" />
                <xs:element name="City" type="xs:string" minOccurs="0" />
                <xs:element name="Longitude" type="xs:float" minOccurs="0" />
                <xs:element name="Latitude" type="xs:float" minOccurs="0" />
                <xs:element name="TimeZone" type="xs:string" minOccurs="0" />
                <xs:element name="Code" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table>
    <CityId>27239</CityId>
    <CountryId>254</CountryId>
    <RegionId>124</RegionId>
    <City>Gray Mountain</City>
    <Longitude>35.8756</Longitude>
    <Latitude>-111.412</Latitude>
    <TimeZone>-07:00</TimeZone>
    <Code>753</Code>
  </Table>
  <Table>
    <CityId>35786</CityId>
    <CountryId>254</CountryId>
    <RegionId>148</RegionId>
    <City>Rapelje</City>
    <Longitude>45.9932</Longitude>
    <Latitude>-109.288</Latitude>
    <TimeZone>-07:00</TimeZone>
    <Code>756</Code>
  </Table>
  <Table>
    <CityId>4354</CityId>
    <CountryId>122</CountryId>
    <RegionId>2329</RegionId>
    <City>Tamana</City>
    <Longitude>32.917</Longitude>
    <Latitude>130.567</Latitude>
    <TimeZone>+09:00</TimeZone>
    <Code />
  </Table>


now on cityid
35786
which is on the second row i want to edit the city value from
Rapelje
to NewCity how am i going to accomplish that? please help thanks
Posted

As soon as your XML file is quite small, I would first advise the solution based on DOM (see first item from the list below); other solutions can also be considered. Below, I overview them:


  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


—SA
 
Share this answer
 
Comments
Abhinav S 16-Oct-11 0:08am    
Good answer as usual. 5.
Sergey Alexandrovich Kryukov 16-Oct-11 0:15am    
Thank you, Abhinav.
--SA
RaviRanjanKr 16-Oct-11 17:19pm    
Nice Answer, My 5+
Sergey Alexandrovich Kryukov 16-Oct-11 20:22pm    
Thank you, Ravi.
--SA
I suggest HtmlAgilityPack

http://htmlagilitypack.codeplex.com/[^]

It is fantastic! With knowing XPath, do whatever you want with XML files or even ASPX or ASCX files which their format will confuse Microsoft XMLDocument. but HtmlAgilityPack can tolerate erroneous XML files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900