2

I need to get clear xml without any namespace and type declarations. Here is the serialized xml:

<placeBetRequest p1:type="PlaceBetRequestTeamed" xmlns:p1="http://www.w3.org/2001/XMLSchema-instance">
...
</placeBetRequest>

Need to someway configure XmlSerializer to set it don't add any attributes but only mine (if I set them with [XmlAttribte] the clean xml need to looks like this:

<placeBetRequest>
...
</placeBetRequest>

Here is my serialization method:

public static string XmlConvert<T>(T obj, params Type[] wellKnownTypes) where T : class
{
    var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
    var settings = new XmlWriterSettings {Indent = true, OmitXmlDeclaration = true};
    XmlSerializer serializer = wellKnownTypes == null
        ? new XmlSerializer(typeof(T))
        : new XmlSerializer(typeof(T), wellKnownTypes);
    using (var stream = new StringWriter())
    using (var writer = XmlWriter.Create(stream, settings))
    {
        serializer.Serialize(writer, obj, emptyNamepsaces);
        return stream.ToString();
    }
}

Please help. Thanks.

Alexey Kulikov
  • 1,097
  • 1
  • 14
  • 38

1 Answers1

-1

I've been using regex

string input = 
    "<p1:placeBetRequest p1:type=\"PlaceBetRequestTeamed\" xmlns:p1=\"http://www.w3.org/2001/XMLSchema-instance\">" +
    "</p1:placeBetRequest>";

    string pattern = @"(?'prefix'\</?)(?'ns'[^:]*:)";
    string output = Regex.Replace(input, pattern, "${prefix}");
Termininja
  • 6,620
  • 12
  • 48
  • 49
jdweng
  • 33,250
  • 2
  • 15
  • 20