第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

C# 刪除屬性 XML

C# 刪除屬性 XML

C#
慕運(yùn)維8079593 2021-07-27 05:06:22
我試圖從下面的 XML 文件示例代碼中刪除一些指定的屬性。string[] szNodeList 是數(shù)組列表,因此節(jié)點(diǎn)包含字符串?dāng)?shù)組中的名稱將被刪除并再次保存任何幫助將不勝感激。        var doc = new System.Xml.XmlDocument();        doc.Load("attrs.xml");        var root = doc.DocumentElement;        string[] szNodeList = new string[]  { "titleTextColor"        ,"isLightTheme"        ,"showText"                    };        foreach (System.Xml.XmlElement  child in root )        {            foreach (string sz in szNodeList)            {                root.RemoveAttribute(sz);                //if (child.Attributes[sz] != null)                //{                //    child.Attributes.Remove(child.Attributes[sz]);                //}            }        }        doc.Save("build.xml");    XML CODE  <?xml version="1.0" encoding="utf-8"?>  <resources>    <attr name="cropImageStyle" format="reference" />    <attr name="drawerArrowStyle" format="reference" />    <attr name="height" format="dimension" />    <attr name="isLightTheme" format="boolean" />    <attr name="title" format="string" />    <attr name="navigationMode">      <enum name="listMode" value="1" />      <enum name="normal" value="0" />      <enum name="tabMode" value="2" />    </attr>  </resources>但是保存為原始文件而不更改我刪除的東西不起作用。
查看完整描述

2 回答

?
天涯盡頭無女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

試試這個(gè):


doc

    // select all `resources/attr` node

    .SelectNodes("resources/attr")

    .Cast<XmlNode>()

    // that contains the `name` attribute whose value is in `szNodeList`

    .Where(x => !string.IsNullOrEmpty(x.Attributes["name"]?.Value) && szNodeList.Contains(x.Attributes["name"].Value))

    .ToList()

    // and, remove them from their parent

    .ForEach(x => x.ParentNode.RemoveChild(x));


查看完整回答
反對(duì) 回復(fù) 2021-07-31
?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

這里的問題之一是術(shù)語。正如我所理解的,您不是在嘗試刪除屬性- 您是在嘗試根據(jù)屬性的值刪除整個(gè)元素name。


如果您可以為此使用 LINQ to XML,我會(huì)這樣做。它通常使使用 XML 變得更加容易。這是一個(gè)完整的程序來做你想做的事:


using System;

using System.Linq;

using System.Xml.Linq;


class Test

{

    static void Main()

    {

        var namesToRemove = new[]

        { 

            "titleTextColor",

            "isLightTheme",

            "showText"

        };

        XDocument doc = XDocument.Load("test.xml");

        // For all the elements directly under the document root...

        doc.Root.Elements()

            // Where the array contains the value of the "name" attribute...

            .Where(x => namesToRemove.Contains((string) x.Attribute("name")))

            // Remove them from the document

            .Remove();

        doc.Save("output.xml");

    }

}

輸出:


<?xml version="1.0" encoding="utf-8"?>

<resources>

  <attr name="cropImageStyle" format="reference" />

  <attr name="drawerArrowStyle" format="reference" />

  <attr name="height" format="dimension" />

  <attr name="title" format="string" />

  <attr name="navigationMode">

    <enum name="listMode" value="1" />

    <enum name="normal" value="0" />

    <enum name="tabMode" value="2" />

  </attr>

</resources>


查看完整回答
反對(duì) 回復(fù) 2021-07-31
  • 2 回答
  • 0 關(guān)注
  • 306 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)