开发者

XElement creation issue

开发者 https://www.devze.com 2023-01-20 16:40 出处:网络
I\'m trying to create a structure like <?xml version=\"1.0\" encoding=\"UTF-8\"?> <urlset xmlns=\"namespace1\"

I'm trying to create a structure like

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="namespace1"
        xmlns:image="namespace2">
  <url> 
    <loc>http://www.example.com/foo.html</lo开发者_开发百科c> 
    <image:image>
       <image:loc>http://example.com/image.jpg</image:loc> 
    </image:image>
  </url>
</urlset>

Any ideas on how to create the image elements using XLinq?

Thanks


You're looking for the XNamespace class.

For example:

XNamespace image = "namespace2";

var element = new XElement(image + "image",
    new XElement(image + "loc", someUrl)
);


I'm not sure if you can get exactly what your after, but this:

XNamespace ns1 = "namespace1";
XNamespace ns2 = "namespace2";

new XElement(ns1 + "urlset",
    new XElement(ns1 + "loc", "http://www.example.com/foo.htm"),
    new XElement(ns2 + "image",
        new XElement(ns2 + "loc", "http://example.com/image.jpg"))).Dump();

Should get you the equivalent.

<urlset xmlns="namespace1">
  <loc>http://www.example.com/foo.htm</loc>
  <image xmlns="namespace2">
    <loc>http://example.com/image.jpg</loc>
  </image>
</urlset> 
0

精彩评论

暂无评论...
验证码 换一张
取 消