Delete empty xmlns created using serializer

I have an object generated using the operation "Add a service link ..." and I manually serialize it using the serializer generator that I wrote.

My problem is that there are some internal objects in the data contract.

The serializer adds an empty namespace to the source tag of internal objects. Is there any way to stop this?

+4
source share
3 answers

How to make your internal objects belong to the same namespace as the root? Thus, it would be correct to omit the xmlns declaration from descendants. You can use the [assembly: ContractNamespace] attribute to override the namespace for all contracts in your assembly. For an example, see Data Contract Names .

Edit : development with some examples below.

Suppose you create an XML document manually and do not specify a namespace for any of your elements.

 XDocument xmlDocument = new XDocument( new XElement("Book", new XElement("Title", "Animal Farm"), new XElement("Author", "George Orwell"), new XElement("Publisher", new XElement("Name", "Secker and Warburg"), new XElement("Location", "London"), new XElement("Founded", 1910)))); return xmlDocument.ToString(); 

The generated XML, as expected, will be stripped of namespace declarations:

 <Book> <Title>Animal Farm</Title> <Author>George Orwell</Author> <Publisher> <Name>Secker and Warburg</Name> <Location>London</Location> <Founded>1910</Founded> </Publisher> </Book> 

If, however, you specify a namespace only for your root element, then all child elements must explicitly return from this default namespace using the declaration xml="" . According to the Default Namespace Rules :

The default declared namespace extends from the start of the start tag, in which it is displayed to the end of the corresponding end tag, excluding the scope of any internal default namespace declarations. In the case of an empty tag, the area is the tag itself.

So the following code (having the namespace specified for the root element) ...

 XDocument xmlDocument = new XDocument( new XElement("{http://example.com/library}Book", new XElement("Title", "Animal Farm"), new XElement("Author", "George Orwell"), new XElement("Publisher", new XElement("Name", "Secker and Warburg"), new XElement("Location", "London"), new XElement("Founded", 1910)))); return xmlDocument.ToString(); 

... will provide the following XML:

 <Book xmlns="http://example.com/library"> <Title xmlns="">Animal Farm</Title> <Author xmlns="">George Orwell</Author> <Publisher xmlns=""> <Name>Secker and Warburg</Name> <Location>London</Location> <Founded>1910</Founded> </Publisher> </Book> 

Note that the children of the <Publisher> element do not need to be returned from the root namespace, because they inherit the "no namespace" declaration from their parent.

To exclude xmlns="" declarations, we could, for the sake of demonstration, assign the same namespace to all descendants:

 XDocument xmlDocument = new XDocument( new XElement("{http://example.com/library}Book", new XElement("{http://example.com/library}Title", "Animal Farm"), new XElement("{http://example.com/library}Author", "George Orwell"), new XElement("{http://example.com/library}Publisher", new XElement("{http://example.com/library}Name", "Secker and Warburg"), new XElement("{http://example.com/library}Location", "London"), new XElement("{http://example.com/library}Founded", 1910)))); return xmlDocument.ToString(); 

This will produce an XML document with a namespace declared only in the root (and implicitly inherited in all descendants):

 <Book xmlns="http://example.com/library"> <Title>Animal Farm</Title> <Author>George Orwell</Author> <Publisher> <Name>Secker and Warburg</Name> <Location>London</Location> <Founded>1910</Founded> </Publisher> </Book> 

To mimic your scenario related to a web service, we can create the following WCF service.

 [DataContract] public class Book { [DataMember] public string Title { get; set; } [DataMember] public string Author { get; set; } [DataMember] public Publisher Publisher { get; set; } } [DataContract] public class Publisher { [DataMember] public string Name { get; set; } [DataMember] public string Location { get; set; } [DataMember] public short Founded { get; set; } } [ServiceContract] public interface ILibraryService { [OperationContract] Book GetBook(); } public class LibraryService : ILibraryService { public Book GetBook() { return new Book { Title = "Animal Farm", Author = "George Orwell", Publisher = new Publisher { Name = "Secker and Warburg", Location = "London", Founded = 1910, } }; } } 

We add the service link to the above service in our client application, consume its operation and serialize the result, enclosing it in the root Books element, which has an explicit namespace:

 using (var libraryClient = new LibraryServiceReference.LibraryServiceClient()) { var book = libraryClient.GetBook(); var stringBuilder = new StringBuilder(); using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder)) { xmlWriter.WriteStartElement("Books", "http://example.com/library"); var serializer = new XmlSerializer(book.GetType()); serializer.Serialize(xmlWriter, book); xmlWriter.WriteEndElement(); } return stringBuilder.ToString(); } 

In this case, the internal Book element contains the declaration xmlns="" .

 <?xml version="1.0" encoding="utf-16"?> <Books xmlns="http://example.com/library"> <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns=""> <ExtensionData /> <Author>George Orwell</Author> <Publisher> <ExtensionData /> <Founded>1910</Founded> <Location>London</Location> <Name>Secker and Warburg</Name> </Publisher> <Title>Animal Farm</Title> </Book> </Books> 

As explained above, this xmlns="" can be eliminated by setting the namespace of the Book elements (and its descendants) according to the roots. For the XmlSerializer class, the default namespace for all elements can be specified through its second parameter to its constructor. (The actual technique depends on what serialization strategy you use.)

 using (var libraryClient = new LibraryServiceReference.LibraryServiceClient()) { var book = libraryClient.GetBook(); var stringBuilder = new StringBuilder(); using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder)) { xmlWriter.WriteStartElement("Books", "http://example.com/library"); var serializer = new XmlSerializer(book.GetType(), "http://example.com/library"); serializer.Serialize(xmlWriter, book); xmlWriter.WriteEndElement(); } return stringBuilder.ToString(); } 

And this will give the expected result:

 <?xml version="1.0" encoding="utf-16"?> <Books xmlns="http://example.com/library"> <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ExtensionData /> <Author>George Orwell</Author> <Publisher> <ExtensionData /> <Founded>1910</Founded> <Location>London</Location> <Name>Secker and Warburg</Name> </Publisher> <Title>Animal Farm</Title> </Book> </Books> 
+6
source

If you have control over the serializer, you can always add an empty namespace to exclude xmlns from the XML output. For instance:

 var serializer = new XmlSerializer(target.GetType()); var ns = new XmlSerializerNamespaces(); ns.Add("",""); serializer.Serialize(xmlWriter, target, ns); 

Yours faithfully,

0
source

This may be a little off topic and may not apply if you use [DataContract] . Rather, this refers to the proxy code generated from WSDL (in the java endpoint interaction environment, we were told that xmlns = "" is invalid). So I post it there if that helps.

The XmlElementAttribute.Form property can call xmlns = "" for children in a WCF request when setting System.Xml.Schema.XmlSchemaForm.Unqualified .

 [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string MyProperty { get; set; } 

which produces something along the lines

 <MyObject xmlns="http://some.namespance"> <MyProperty xmlns="">My value goes here</MyProperty> </MyObject> 

Setting the value to System.Xml.Schema.XmlSchemaForm.None (which is the default) means that it will not display the "unskilled" namespace attribute.

 [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.None)] public string MyProperty { get; set; } 

which produces this:

 <MyObject xmlns="http://some.namespance"> <MyProperty>My value goes here</MyProperty> </MyObject> 

I'm not sure if you can change this behavior when importing a wsdl link or maybe change wsdl, but I finished editing the generated proxy code directly (which is definitely not perfect), but my immediate goal was achieved.

0
source

Source: https://habr.com/ru/post/1390472/


All Articles