代码之家  ›  专栏  ›  技术社区  ›  Michael

为什么XML节点值不变?

  •  2
  • Michael  · 技术社区  · 6 年前

    我有这个XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <LayerDefinition version="1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LayerDefinition-1.0.0.xsd">
      <VectorLayerDefinition>
        <ResourceId>ddddd</ResourceId>
        <FeatureName>SHP_Schema:HydrographicPolygons</FeatureName>
        <FeatureNameType>FeatureClass</FeatureNameType>
        <Geometry>SHPGEOM</Geometry>
        <VectorScaleRange>
          <AreaTypeStyle>
            <AreaRule>
              <LegendLabel/>
              <AreaSymbolization2D>
                <Fill>
                  <FillPattern>Solid</FillPattern>
                  <ForegroundColor>FFABC7E9</ForegroundColor>
                  <BackgroundColor>FF000000</BackgroundColor>
                </Fill>
                <Stroke>
                  <LineStyle>Solid</LineStyle>
                  <Thickness>0</Thickness>
                  <Color>FFABC7E9</Color>
                  <Unit>Inches</Unit>
                </Stroke>
              </AreaSymbolization2D>
            </AreaRule>
          </AreaTypeStyle>
        </VectorScaleRange>
      </VectorLayerDefinition>
    </LayerDefinition>
    

    我需要更改此元素:

    <BackgroundColor>FF000000</BackgroundColor>
    

    对此:

    <BackgroundColor>FFFFAAAA</BackgroundColor>
    

    以下是我尝试的方法:

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(layoutXml);
    XmlNodeList objNodeList = doc.SelectNodes("VectorLayerDefinition/VectorScaleRange/BackgroundColor");
    
     objNodeList.InnerXml = "FFFFAAAA";
    

    但是上面的代码是有效的。我做错了什么?为什么态度不起作用?

    3 回复  |  直到 6 年前
        1
  •  2
  •   jdweng    6 年前

    使用xml linq:

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication75
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
                XNamespace ns = doc.Root.GetDefaultNamespace();
    
                XElement backgrounColor = doc.Descendants(ns + "BackgroundColor").FirstOrDefault();
                backgrounColor.SetValue("FFFFAAAA");
    
            }
    
        }
    
    }
    
        2
  •  1
  •   E McG    6 年前

    我认为你在你的节点上走得不够远。我想你需要更像这样的东西。

     XmlNodeList objNodeList = doc.SelectNodes("VectorLayerDefinition/VectorScaleRange/AreaTypeStyle/AreaRule/AreaSymbolization2D/Fill");
    
     objNodeList.selectSingleNode("BackgroundColor").innerXml= "FFFFAAAA";
    

    selectSingleNode() 有什么用 BackGroundColor 内部节点列表中的节点 Fill

        3
  •  0
  •   nalnpir    6 年前

    编辑了你的问题的答案

            XmlDocument doc = new XmlDocument();
            doc.Load("texto.xml");  
    
            XmlNodeList objNodeList = doc.SelectNodes("/LayerDefinition/VectorLayerDefinition/VectorScaleRange/AreaTypeStyle/AreaRule/AreaSymbolization2D/Fill");
    
            objNodeList.Item(0).SelectSingleNode("BackgroundColor").InnerXml = "FFFFAAAA";
    
    
            doc.Save("texto.xml");