所以,我还是问到这个主题的问题: - (
So I am still asking questions about this topic :-(
所以,我创建了一个对象,用XML序列化装饰它的属性,从我所看到的我添加一个空的命名空间的XML序列化namepsace集合,以免让多余的属性,我也没打算有。
So I create an object, decorate it with the Xml Serialization Attributes, from what I have seen I add an empty namespace to the xml serialization namepsace collections so as not to get the superfluous attributes I did not intend to have.
编辑:我的意思是属性是这些:
The attribute I mean are these:
<url xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:xsd="http://www.w3.org/2001/XMLSchema"; xmlns="">
所以这给了我两个额外的属性。
so it gives me two extra attributes.
如果我更改文档的开头从经过进一步调查:**
After further investigation if I change the beginning of the document from:**
writer.WriteStartElement("urlset","http://www.sitemaps.org/schemas/sitemap/0.9");
到
writer.WriteStartElement("urlset");
**然后,我没有得到空的xmlns =属性中的URL标记。这是伟大的,但我确实需要的根元素具有的xmlns =http://www.sitemaps.org/schemas/sitemap/0.9
,即:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
但我仍然得到一个空的的xmlns =
中的序列化类型属性。
But I still get an empty xmlns=""
attribute in the serialized type.
[XmlRoot(ElementName = "url", Namespace="")]
public class SitemapNode
{
[XmlElement(ElementName = "loc")]
public string Location { get; set; }
[XmlElement(ElementName = "lastmod")]
public DateTime LastModified { get; set; }
[XmlElement(ElementName = "changefreq")]
public SitemapChangeFrequency ChangeFrequency { get; set; }
[XmlElement(ElementName = "priority")]
public decimal Priority { get; set; }
public SitemapNode()
{
Location = String.Empty;
LastModified = DateTime.Now;
ChangeFrequency = SitemapChangeFrequency.monthly;
Priority = 0.5M;
}
public SitemapNode(string location, DateTime lastModified, SitemapChangeFrequency changeFrequency, decimal priority)
{
Location = location;
LastModified = lastModified;
ChangeFrequency = changeFrequency;
Priority = priority;
}
}
然后我用下面的追加到我的XmlWriter:
Then I use the following to append to my XmlWriter:
foreach (uk.co.andrewrea.SitemapNode node in List)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(String.Empty, String.Empty);
Serializer.Serialize(Writer, node, ns);
}
这工作了罚款只是我留下的EMPTY的的xmlns =像这样
This works out fine except I am left with an emtpy xmlns="" like this
<url xmlns="">
任何人任何想法?再次,我可以用的XmlTextWriter和XmlDocument的做到这一点,但我需要用的XmlWriter去实现它。
Anyone any ideas? Again I can achieve this using the XmlTextWriter and the XmlDocument but I need to achieve it using the XmlWriter.
任何帮助是极大的AP preciated。
Any help is greatly appreciated.
这工作(你只需要他们在同一个命名空间和使用命名空间类,以便在writter不会混淆):
This works (you just need them to be in the same namespace and you use the namespaces class so the writter doesn't confuse):
[TestMethod]
public void TestMethod3()
{
var list = new []{new SitemapNode("1", DateTime.Now, 1), new SitemapNode("2", DateTime.Now.AddDays(1), 2)};
var serializer = new XmlSerializer(typeof(SitemapNode));
var st = new MemoryStream();
using (var writer = XmlWriter.Create(st))
{
var ns = new XmlSerializerNamespaces();
ns.Add("", "test");
writer.WriteStartElement("test", "test");
foreach (SitemapNode node in list)
{
serializer.Serialize(writer, node, ns);
}
writer.WriteEndElement();
}
st.Position = 0;
TestContext.WriteLine(new StreamReader(st).ReadToEnd());
}
[XmlRoot(ElementName = "url", Namespace = "test")]
public class SitemapNode
{
[XmlElement(ElementName = "loc")]
public string Location { get; set; }
[XmlElement(ElementName = "lastmod")]
public DateTime LastModified { get; set; }
[XmlElement(ElementName = "priority")]
public decimal Priority { get; set; }
public SitemapNode()
{
Location = String.Empty;
LastModified = DateTime.Now;
Priority = 0.5M;
}
public SitemapNode(string location, DateTime lastModified, decimal priority)
{
Location = location;
LastModified = lastModified;
Priority = priority;
}
}
和输出(根据您的意见,这就是你要找的人):
And the output is (based on your comments that is what you were looking for):
<?xml version="1.0" encoding="utf-8"?><test xmlns="test">
<url><loc>1</loc><lastmod>2009-03-05T13:35:54.6468-07:00</lastmod><priority>1</priority></url>
<url><loc>2</loc><lastmod>2009-03-06T13:35:54.6478-07:00</lastmod><priority>2</priority></url></test>