所以我有以下代碼,我在其中使用XDocument庫來決議我的XML并在所需的 XML 上執行相關操作。
現在,在洗掉特定塊下的節點時,我觀察到它洗掉了塊的結束標記。請參考此處的作業程式來演示此行為:
- https://dotnetfiddle.net/wmY01V
您會看到我正在嘗試洗掉元素lang下的標簽hl1。它lang成功洗掉了標簽,但也洗掉了</hl1>結束標簽。
為什么會發生這種行為?
這里的另一個問題是我如何在元素下添加一個具有新值的lang節點并洗掉多個節點?hl1
代碼:
using System;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var doc = XDocument.Parse(@"<?xml version='1.0' encoding='utf-8'?>
<!--<!DOCTYPE nitf SYSTEM 'nitf-3-4.dtd'>-->
<nitf>
<head>
</head>
<body>
<body.head>
<hedline>
<hl1 id='Headline1' class='1' style='Headline1' MainHead='true'>
<lang class='3' style='Headline1' font='Patrika15' fontStyle='Bold' size='18'>????????:</lang>
<lang class='3' style='Headline1' font='Patrika15' fontStyle='Bold' size='18'>???????????? ???????????</lang>
</hl1>
<hl2 id='Headline2' class='1' style='Headline2' MainHead='false'>
<lang class='3' style='Headline2' font='Patrika15' fontStyle='Bold' size='30'>53.15 ??? ?????? ?? ???? ????? ?? ??? 3456 ???????</lang>
</hl2>
<hl2 id='Headline2' class='1' style='Headline2' MainHead='false'>
<lang class='3' style='Headline2' font='Patrika15' fontStyle='Bold' size='30'>53.15 ??? ?????? ?? ???? ????? ?? ??? 3456 ??????? 12</lang>
</hl2>
</hedline>
<summary></summary>
<quotes>
<quote></quote>
</quotes>
</body.head>
<body.content id='Bodytext'>
</body.content>
</body>
</nitf>");
var element = doc.XPathSelectElement("//hedline");
int hl1=0;
foreach (XNode node in element.Nodes())
{
if(node.ToString().Contains("<hl1"))
{
hl1 ;
string h1text=string.Empty;
var x = node.XPathSelectElements("lang");
int count = x.Count();
var mylist = x.ToList();
Console.WriteLine(doc);
for(int i=0; i<count;i )
{
mylist[i].Remove();
}
Console.WriteLine("----------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("----------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine(doc);
}
}
}
}
uj5u.com熱心網友回復:
實際上,當您洗掉lang標簽時,hl1標簽會轉換為“ Self-Closing”標簽。這意味著hl1標簽是一個空元素。無論如何嘗試下面的代碼來洗掉hl1標簽的孩子并創建新的。
public static void Main()
{
var doc = XDocument.Parse(@"Your XML content");
//var element = doc.XPathSelectElement("//hedline");
//int hl1 = 0;
foreach (XElement xElement in doc.Descendants("hl1"))
{
#region remove lang tags
var langElements = xElement.XPathSelectElements("lang");
langElements.Remove();
#endregion
#region create new element in this 'hl1' tag
xElement.Add(CreateElement());
#endregion
}
Console.WriteLine(doc);
}
private static XElement CreateElement()
{
XElement xe = new XElement("lang");
xe.SetAttributeValue("class", 3);
xe.SetAttributeValue("style", "Headline1");
xe.SetAttributeValue("font", "Patrika15");
xe.SetAttributeValue("fontStyle", "Bold");
xe.SetAttributeValue("size", "18");
xe.Value = "My Value";
return xe;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/484404.html
上一篇:通知渠道應用程式崩潰并停止
