替换XML中元素的值

时间:2018-11-09 10:29:44

标签: c# xml linq

我需要替换<ResourceId>元素的值。如果我之前删除所有内容 <VectorLayerDefinition>之后,一切正常,但是我有很多此类xml文件,因此无法手动删除任何内容。有什么建议吗?

以下是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="sdf:FeatureClass1">
    <ns0:FeatureSource xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" rdf:resource="fsd://1" />
  </rdf:Description>
  <rdf:Description rdf:about="sdf:FeatureClass1">
    <rdf:type rdf:resource="http://www.autodesk.com/gis/resource/definitions/LayerDefinitionResource" />
  </rdf:Description>
  <rdf:Description rdf:about="sdf:FeatureClass1">
    <ns0:name xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" xml:lang="en" rdf:parseType="Literal">FeatureClass1</ns0:name>
  </rdf:Description>
  <rdf:Description rdf:about="sdf:FeatureClass1">
    <ns0:isURL xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" xml:lang="en" rdf:parseType="Literal">no</ns0:isURL>
  </rdf:Description>
  <rdf:Description rdf:about="sdf:FeatureClass1">
    <ns0:description xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" xml:lang="en" rdf:parseType="Literal">
    </ns0:description>
  </rdf:Description>
  <rdf:Description rdf:about="sdf:FeatureClass1">
    <ns0:LayerDefinitionResourceStream xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" xml:lang="en" rdf:parseType="Literal">
      <LayerDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LayerDefinition-2.4.0.xsd" version="2.4.0">
        <VectorLayerDefinition>
          <ResourceId>Library://SDF_1.FeatureSource</ResourceId>
          <FeatureName>sdf:FeatureClass1</FeatureName>
          <FeatureNameType>FeatureClass</FeatureNameType>
          <Geometry>Geometry</Geometry>  
    </VectorLayerDefinition>
      </LayerDefinition>
    </ns0:LayerDefinitionResourceStream>
  </rdf:Description>
  <rdf:Description rdf:about="fsd://1">
    <rdf:type rdf:resource="http://www.autodesk.com/gis/resource/definitions/FeatureSourceResource" />
  </rdf:Description>
  <rdf:Description rdf:about="fsd://1">
    <ns0:name xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" xml:lang="en" rdf:parseType="Literal">1</ns0:name>
  </rdf:Description>
  <rdf:Description rdf:about="fsd://1">
    <ns0:isURL xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" xml:lang="en" rdf:parseType="Literal">no</ns0:isURL>
  </rdf:Description>
  <rdf:Description rdf:about="fsd://1">
    <ns0:description xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" xml:lang="en" rdf:parseType="Literal">
    </ns0:description>
  </rdf:Description>
  <rdf:Description rdf:about="fsd://1">
    <ns0:FeatureSourceResourceStream xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" xml:lang="en" rdf:parseType="Literal">    </ns0:FeatureSourceResourceStream>
  </rdf:Description>
  <rdf:Description rdf:about="LayerSchema">
    <ns0:version xmlns:ns0="http://www.autodesk.com/gis/resource/definitions/" xml:lang="en" rdf:parseType="Literal">1</ns0:version>
  </rdf:Description>
</rdf:RDF>

这是我的代码:

public void Main()
{
    XDocument doc = XDocument.Load(@"D:\Repositories\Library\Content\Layers\FeatureClass1.LayerDefinition");
    string str = doc.Element("VectorLatyerDefinition").Element("ResourceId").Value;
    doc.Element("VectorLayerDefinition").Element("ResourceId").Value = "SomeValue";
    doc.Save(@"D:\Repositories\Library\Content\Layers\FeatureClass1.LayerDefinition");
}

但是在我“清理” xml文件之后它就可以工作了

1 个答案:

答案 0 :(得分:0)

使用xml liinq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication80
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement resourceId = doc.Descendants("ResourceId").FirstOrDefault();
            resourceId.SetValue("//SDF_2.FeatureSource");

        }
    }
}