我正在处理要序列化的C#类,一切正常,除了我疯狂地按如下方式获取根元素的名称空间:
<m:DE815 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:m="http://das.dogane.finanze.it" xmlns="http://das.dogane.finanze.it" versione="1.0">
我需要在根元素内部都写xmlns =“ http://das.dogane.finanze.it”和xmlns:m =“ http://das.dogane.finanze.it”,但是我得到的是这个:
<m:DE815 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:m="http://das.dogane.finanze.it" versione="1.0">
这是我用来序列化我的课程的代码:
stream = new MemoryStream(); // read xml in memory
XmlWriter xmlWriter = XmlWriter.Create(stream);
XmlSerializer serializer = new XmlSerializer(typeof(DE815), "http://das.dogane.finanze.it");
List<XmlQualifiedName> names = new List<XmlQualifiedName>();
var xmlns = new XmlSerializerNamespaces(names.ToArray());
xmlns.Add("m", "http://das.dogane.finanze.it");
xmlns.Add("xs", "http://www.w3.org/2001/XMLSchema");
xmlns.Add("", "http://das.dogane.finanze.it");
serializer.Serialize(xmlWriter, de815, xmlns); // read object
int count = (int)stream.Length; // saves object in memory stream
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
UTF8Encoding utf = new UTF8Encoding(); // convert byte array to string
String xmlAsString = utf.GetString(arr).Trim();
File.WriteAllText("d:\\prova_DE815.xml", xmlAsString, Encoding.UTF8);
这是我要序列化的类的声明:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")
System.SerializableAttribute()
System.Diagnostics.DebuggerStepThroughAttribute()
System.ComponentModel.DesignerCategoryAttribute("code")
System.Xml.Serialization.XmlTypeAttribute(Namespace="http://das.dogane.finanze.it", AnonymousType=true)
System.Xml.Serialization.XmlRootAttribute(Namespace="http://das.dogane.finanze.it", IsNullable=false)
public partial class DE815 {
我在代码上缺少什么?
感谢您的帮助,如果我错过任何信息,请随时提出更多问题。