1

次のような要素を作成しようとしています: "< Info i:nil="true" />

私のコードはそのようなものです

xmlReport の xlist から

             select new XElement("Binding",
                    new XElement("ID", xlist.ID),
                    new XElement("Name", xlist.Name),


                    new XElement("Info"),
                    new XElement("Specification",
                    new XElement("Number", xlist.Number),
                    new XElement("SerialNumber", xlist.SerialNumber),
                    new XElement("Date", xlist.ConsumedDate),
                    new XElement("Site", xlist.Site)))

このコード行は、上記の < Info/> タグの XML を生成しています

< Info i:nil=true/> の代わりに

この "i:nil=true" を取得するにはどうすればよいですか?

4

1 に答える 1

1

これで目的が達成できたようですが、正しい名前空間を入力する必要があります。

using System;
using System.Xml.Linq;

class Program
{    
    public static void Main()
    {
        XNamespace ns = "http://somenamespace";
        XElement element = new XElement("Root",
            new XAttribute(XNamespace.Xmlns + "i", ns.ToString()),
            new XElement("info",
                new XAttribute(ns + "nil", true)));

        Console.WriteLine(element);
    }    
}

最初の属性は、接頭辞「i」をルート要素とその子孫の名前空間「http://somenamespace」に関連付けます。2 番目の属性はその名前空間を使用し、要素が書き出されると、名前空間を表すためにプレフィックスが使用されます。

于 2012-06-13T17:15:02.117 に答える