Comment ajouter un espace de noms par défaut sans préfixe à l'aide de XMLSerializer
Je suis en train de générer un document XML qui contient l'espace de noms par défaut sans préfixe à l'aide de XmlSerializer
par exemple
<?xml version="1.0" encoding="utf-8" ?>
<MyRecord ID="9266" xmlns="http://www.website.com/MyRecord">
<List>
<SpecificItem>
En utilisant le code suivant ...
string xmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(ExportMyRecord));
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add(string.Empty, string.Empty);
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, myRecord, xmlnsEmpty);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlizedString = this.UTF8ByteArrayToString(memoryStream.ToArray());
et la structure de la classe ...
[Serializable]
[XmlRoot("MyRecord")]
public class ExportMyRecord
{
[XmlAttribute("ID")]
public int ID { get; set; }
Maintenant, j'ai essayé les différentes options ...
XmlSerializer xs = new XmlSerializer
(typeof(ExportMyRecord),"http://www.website.com/MyRecord");
ou ...
[XmlRoot(Namespace = "http://www.website.com/MyRecord", ElementName="MyRecord")]
me donne ...
<?xml version="1.0" encoding="utf-8"?>
<q1:MylRecord ID="9266" xmlns:q1="http://www.website.com/MyRecord">
<q1:List>
<q1:SpecificItem>
J'ai besoin de l'XML pour avoir l'espace de noms sans le préfixe comme il va à un tiers fournisseur et ils rejettent d'autres alternatives.
source d'informationauteur OldBob
Vous devez vous connecter pour publier un commentaire.
Là vous allez: