编辑/更新xml内容 [英] edit/update an xml content

查看:71
本文介绍了编辑/更新xml内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想问一下如何使用csharp编辑xml文件中的1个特定内容?假设我有一个具有此值的xml文件

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 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>



现在在第二行的cityid



now on cityid

35786

上,我想将城市值从

Rapelje

编辑到NewCity,我将如何完成该操作?请帮助谢谢.

to NewCity how am i going to accomplish that? please help thanks

推荐答案

一旦您的XML文件很小,我将首先建议基于DOM的解决方案(请参见下面列表中的第一项);也可以考虑其他解决方案.下面,我对其进行概述:

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. 使用System.Xml.XmlDocument类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
    请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [

  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



—SA


我建​​议 HtmlAgilityPack

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

太棒了!了解XPath之后,您可以对XML文件甚至ASPX或ASCX文件进行任何操作,而它们的格式会混淆Microsoft XMLDocument.但是HtmlAgilityPack可以容忍错误的XML文件.
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.


这篇关于编辑/更新xml内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆