LoginSignup
0
1

More than 3 years have passed since last update.

C# で名前空間接頭辞入りの XML を作る

Posted at

C# で名前空間接頭辞入りの XML を作る

この XML を作る.

<Root xmlns="http://example.com/foo">
  <Item1></Item1>
  <Item2 xmlns:i="http://example.com/bar">
    <Item3 />
    <i:Item4 />
  </Item2>
</Root>

その場合のコードは以下のようになる.

var ns1 = XNamespace.Get("http://example.com/foo");
var ns2 = XNamespace.Get("http://example.com/bar");
var xml =
    new XElement(ns1 + "Root",
        new XElement(ns1 + "Item1", ""),
        new XElement(ns1 + "Item2", new XAttribute(XNamespace.Xmlns + "i", ns2),
            new XElement(ns1 + "Item3"),
            new XElement(ns2 + "Item4")));
Console.WriteLine(xml.ToString());

大きな XML を吐こうと思うと便利なんだろうけど、小さい XML を吐く分にはまだるっこしい感.

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1