-2

ドキュメント内の属性「nil = true」を使用して Xml 要素を消去しようとしています。このアルゴリズムを思いついたのですが、見た目が好きではありません。

このアルゴリズムの linq バージョンを知っている人はいますか?

    /// <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);
        }
    }
4

4 に答える 4

1

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();
}
于 2012-02-10T15:57:25.913 に答える
0

拡張メソッド:

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);

出力:

<Root nil="false">
  <b>2</b>
  <c nil="false">
    <e nil="false">4</e>
  </c>
</Root>

ご覧のとおり、ノード<a><d>場所は期待どおりに削除されています。<Root>注意すべき唯一のことは、ルートノードを削除できないため、ノードでこのメソッドを呼び出すことができず、次の実行時エラーが発生することです。

親が行方不明です。

于 2012-02-10T11:12:24.520 に答える
0

これはうまくいくはずです..

public static void CleanNil(this XElement value)
{
     var todelete = value.DescendantsAndSelf().Where(x => (bool?) x.Attribute("nil") == true);
     if(todelete.Any())
     {
        todelete.Remove();
     }
}
于 2012-02-10T10:54:40.293 に答える
0

その問題を解決する方法を変更し、null 許容型で nil を作成しないようにします。次を使用します

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=vs.90%29.aspx

    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;
    }
于 2012-03-23T11:02:19.920 に答える