C# 用attribute“清除Xml元素;nil=真";-创建linq版本?
我试图清除文档中属性为“nil=true”的Xml元素。 我想出了这个算法,但我不喜欢它的样子 有人知道这个算法的linq版本吗C# 用attribute“清除Xml元素;nil=真";-创建linq版本?,c#,linq-to-xml,xml-nil,C#,Linq To Xml,Xml Nil,我试图清除文档中属性为“nil=true”的Xml元素。 我想出了这个算法,但我不喜欢它的样子 有人知道这个算法的linq版本吗 /// <summary> /// Cleans the Xml element with the attribut "nil=true". /// </summary> /// <param name="value">The value.</param> public stati
/// <summary>
/// Cleans the Xml element with the attribut "nil=true".
/// </summary>
/// <param name="value">The value.</param>
public static void CleanNil(this XElement value)
{
List<XElement> toDelete = new List<XElement>();
foreach (var element in value.DescendantsAndSelf())
{
if (element != null)
{
bool blnDeleteIt = false;
foreach (var attribut in element.Attributes())
{
if (attribut.Name.LocalName == "nil" && attribut.Value == "true")
{
blnDeleteIt = true;
}
}
if (blnDeleteIt)
{
toDelete.Add(element);
}
}
}
while (toDelete.Count > 0)
{
toDelete[0].Remove();
toDelete.RemoveAt(0);
}
}
//
///清除属性为“nil=true”的Xml元素。
///
///价值。
公共静态void CleanNil(此元素值)
{
List toDelete=新列表();
foreach(value.degenantsandself()中的var元素)
{
if(元素!=null)
{
bool blnDeleteIt=false;
foreach(element.Attributes()中的var attribute)
{
if(attribute.Name.LocalName==“nil”&&attribute.Value==“true”)
{
blnDeleteIt=true;
}
}
如果(blndeletteit)
{
添加(元素);
}
}
}
而(toDelete.Count>0)
{
toDelete[0]。删除();
toDelete.RemoveAt(0);
}
}
这应该行得通
public static void CleanNil(this XElement value)
{
var todelete = value.DescendantsAndSelf().Where(x => (bool?) x.Attribute("nil") == true);
if(todelete.Any())
{
todelete.Remove();
}
}
扩展方法:
public static class Extensions
{
public static void CleanNil(this XElement value)
{
value.DescendantsAndSelf().Where(x => x.Attribute("nil") != null && x.Attribute("nil").Value == "true").Remove();
}
}
示例用法:
File.WriteAllText("test.xml", @"
<Root nil=""false"">
<a nil=""true""></a>
<b>2</b>
<c nil=""false"">
<d nil=""true""></d>
<e nil=""false"">4</e>
</c>
</Root>");
var root = XElement.Load("test.xml");
root.CleanNil();
Console.WriteLine(root);
File.WriteAllText(“test.xml”,@”
2.
4.
");
var root=XElement.Load(“test.xml”);
root.CleanNil();
控制台写入线(根);
输出:
<Root nil="false">
<b>2</b>
<c nil="false">
<e nil="false">4</e>
</c>
</Root>
2.
4.
如您所见,节点
和
按预期移除。唯一需要注意的是,您不能在
节点上调用此方法,因为无法删除根节点,您将得到以下运行时错误:
家长不见了
nil
属性的名称空间是什么?将其放入{}中,如下所示:
public static void CleanNil(this XElement value)
{
value.Descendants().Where(x=> (bool?)x.Attribute("{http://www.w3.org/2001/XMLSchema-instance}nil") == true).Remove();
}
我改变了解决这个问题的方法,并避免在我的可空类型上创建nil 我使用以下方法
@弗兰克你说它不起作用是什么意思?我在答案中添加了一个工作示例。我对它进行了测试,它可以工作。您的问题没有提到任何关于XML名称空间的内容,但是名称空间(通常使用前缀
xsi
)有一个nil
属性,您可以为nil元素设置true
。如果这是您正在使用的属性,它将解释为什么答案不能按预期工作。答案会在默认名称空间中查找nil
属性,不会与XML一起使用。
public class OptionalOrder
{
// This field should not be serialized
// if it is uninitialized.
public string FirstOrder;
// Use the XmlIgnoreAttribute to ignore the
// special field named "FirstOrderSpecified".
[System.Xml.Serialization.XmlIgnoreAttribute]
public bool FirstOrderSpecified;
}