XML中加入namespace

实际的显示效果是这样的

<?xml version="1.0" encoding="GB2312"?>

<document xmlns:bbs="http://www.baidu.com/search/bbs_sitemap.xsd">

<webSite>bbs.diaoyu.com</webSite>

<webMaster>[email protected]</webMaster>

<updatePeri>0.5</updatePeri>

<updatetime>2012/11/16 00:00:00</updatetime>

<version>钓鱼之家论坛</version>

<item>

<link>http://bbs.diaoyu.com/showtopic-2585.aspx</link>

<title>化氏秘籍</title>

<pubDate>2012-11-13 18:13:08</pubDate>

<bbs:lastDate>2012-11-16 10:09:08</bbs:lastDate>

<bbs:reply>4</bbs:reply>

<bbs:hit>1</bbs:hit>

<bbs:mainLen>187</bbs:mainLen>

<bbs:boardid>2</bbs:boardid>

<bbs:pick>0</bbs:pick>

</item>

</document>
XDocument xdoc = new XDocument();

            xdoc.Declaration = new XDeclaration("1.0", "GB2312","yes");

            XElement root = new XElement("document", new XAttribute(XNamespace.Xmlns+"bbs", "http://www.baidu.com/search/bbs_sitemap.xsd"));
private static XElement GetItem(TopicInfo data)

        {

            XNamespace xn = "http://www.baidu.com/search/bbs_sitemap.xsd";



            byte[] MessageByte = System.Text.Encoding.Default.GetBytes(data.Message);

            int len = MessageByte.Length;

            int pick = Convert.ToInt32(data.Digest) > 0 ? 1 : 0;

            XElement item = new XElement("item",

                new XElement("link", "http://bbs.diaoyu.com/showtopic-2585.aspx"),

                new XElement("title",data.Title),

                new XElement("pubDate", data.PostDateTime.ToString("yyyy-MM-dd HH:mm:ss")),

                new XElement(xn+"lastDate", data.LastPost.ToString("yyyy-MM-dd HH:mm:ss")),

                new XElement(xn+"reply",data.Replies),

                new XElement (xn+"hit",data.Views),

                new XElement(xn+"mainLen",len),

                new XElement(xn+"boardid",data.Fid),

                new XElement(xn+"pick",pick)

                );

            return item;

        }

显示的结果就是在每个加了”xn“的XElement,前面会显示bbs:,如果把

new XAttribute(XNamespace.Xmlns+"bbs", "http://www.baidu.com/search/bbs_sitemap.xsd")中的bbs改成别的,那么那些加了”xn“的XElement会显示更改之后的namespace

你可能感兴趣的:(namespace)