What common collections in C # are IXmlSerializable?

Are any of the common .NET collections labeled IXmlSerializable? I tried List <T> and Collection <T> but do not work out of the box.

Before flipping my own collection <T>, list <T>, or dictionary <T> class, I thought I would check if Microsoft has included something that does this already. This is similar to the basic functionality.

EDIT: By "rolling my own," I mean creating a class that inherits from the general collection class and also implements IXmlSerializable. Here is one example: http://www.codeproject.com/KB/XML/IXmlSerializable.aspx . And here is another example: http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx .

I use a DataContractSerializer in a method whose signature looks like this:

public static Stream GetXmlStream(IXmlSerializable item)

The problem is that while there are many classes in the .NET environment that can be serialized, not all of them explicitly implement the IXmlSerializable interface.

+3
source share
4 answers

As far as I know, they are not. However, you can take a look at the attached link. I think you find this useful.

Serialize and deserialize objects as Xml using common types in C # 2.0

+4
source

There is no need to implement IXmlSerializable for be xml serializable.

public class Foo 
{
    public List<string> Names { get; set; }
}

xml will serialize just fine, creating something like:

<?xml version="1.0" encoding="utf-16"?>
<Foo 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Names>
    <string>a</string>
    <string>b</string>
    <string>c</string>
  </Names>
</Foo>

whereas

public class Foo<T> 
{
    public List<T> Names {  get; set; }
}

will create

<?xml version="1.0" encoding="utf-16"?>
<FooOfString 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Names>
    <string>a</string>
    <string>b</string>
    <string>c</string>
  </Names>
</FooOfString>
+5

In connection with your question, if you want to serialize / deserialize IDictionary, see this useful post . I would use the template shown here for your read and write methods, although they do better in various situations.

+2
source

After some further research, I came to the same conclusion. None of the common .NET collections are IXmlSerializable with .NET 3.5 SP1.

+1
source

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


All Articles