0

プレフィックス g を使用して xml ドキュメントを作成しようとしています:

c#

 static void Main(string[] args)
        {
            XNamespace g = "g:";
            XElement contacts =
    new XElement("Contacts",
        new XElement("Contact",
            new XElement( g+"Name", "Patrick Hines"),
            new XElement("Phone", "206-555-0144"),
            new XElement("Address",
                new XElement("street","this street"))               

        )
    );
            Console.WriteLine(contacts);
        }

代わりに、次のように表示されます。

..<contacts>
  <contact>
    <name xmlns="g:">
...
4

3 に答える 3

0

XDocumentSystem.Xml.Linq名前空間にあります。したがって、コード ファイルの先頭に以下を追加します。

using System.Xml.Linq;

次に、次の方法でファイルにデータを書き込むことができます。

XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
            new System.Xml.Linq.XElement("Contacts"),
            new XElement("Contact",
            new XElement("Name", c.FirstOrDefault().DisplayName),
            new XElement("PhoneNumber", c.FirstOrDefault().PhoneNumber.ToString()),
            new XElement("Email", "abc@abc.com"));
doc.Save(...);
于 2013-08-27T05:42:30.883 に答える