.net - Serializing object with no namespaces using DataContractSerializer -


Can I extract XML namespace from XML representation of an object serial using DataContractSerializer?

That item needs a very simple output can be serial on XML.

  • Latest & amp;
  • XML should not be Aksmelan in ... ... namespace riffs
  • best - .net 4 beta using 2
  • deserialized object Will not be required.
  • Exceptions and any subtypes of ISubObject should be supported.
  • The original object will be very difficult to change.

Object:

  [serialable] Class MyObj {string str; Exception preceded; ISubObject subobage; }  

should be sorted in:

  & lt; Xml & gt; & Lt; Str & gt; ... & lt; / Str & gt; & Lt; Ex i: nil = "true" /> & Lt; Subobj i: type = "abc" & gt; & Lt; AbcProp1 & gt; ... & lt; / AbcProp1 & gt; & Lt; AbcProp2 & gt; ... & lt; / AbcProp2 & gt; & Lt; / Subobj & gt; & Lt; / XML & gt;  

I used this code:

  private static string ObjectToXmlString (object obj) {if (obj == null) throw new ArgumentNullException ( "obj "); Var serializer = new data Kantrektsiriyalaijr (ObjkGetType (), NULL, Int32kMaxValue, false, false, empty, new permitting the Place all Kantentretrolvr ()); Var sb = new stringbilder (); While the use (VAR xw = XmlWriter.Create (sb, New XmlWriterSettings {OmitXmlDeclaration = true, NamespaceHandling = NamespaceHandling.OmitDuplicates, indent = true})) {serializer.WriteObject (xw, obj); Xw.flush (); Return sb.ToString (); }}  

I adopted a DataContractResolver to a subtype to be announced:

  public class AllowAllContractResolver: DataContractResolver {public override bool TryResolveType (type dataContractType, Type declareType, DataContractResolver knownTypeResolver, XmlDictionaryString typeName out, XmlDictionaryString typeNamespace outside {if} {var = new XmlDictionary () (knownTypeResolver.TryResolveType (declarativeTypeResolver.TryResolveType, declareType, null, out typeName, typeNamespace!); TypeName = DataContractType.FullName); Type Namespace = Add Dictionary (Data Contact. To form. Assembly.FullName);} return true;} public override Type ResolveName (string TypeName, String typeNamespace, type declaredType, DataContractResolver knownTypeResolver) {return knownTypeResolver.ResolveName (TypeName, typeNamespace, declaredType, NULL) ?? Type.GetType ( TypeName + "," + type namespace);}}  

You marked those sections You will want to sort them with:

[DataContract (namespace = "")]

P> mark


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -