Мне нужен XML без ссылок на xmlns.
это мой пример :
Код: Выделить всё
[Serializable]
public class Root
{
[XmlElement("Tables")]
public List Tables { get; set; } = new List();
}
[XmlInclude(typeof(TableType1))]
[XmlInclude(typeof(TableType2))]
[Serializable]
public class AbstractTable
{
[XmlAttribute]
public string TableName { get; set; }
}
[Serializable]
public class TableType1 : AbstractTable
{
public TableType1()
{
TableName = "TableType1";
}
[XmlAttribute]
public string ValueA { get; set; }
}
[Serializable]
public class TableType2 : AbstractTable
{
public TableType2()
{
TableName = "TableType2";
}
[XmlAttribute]
public string ValueB { get; set; }
}
class Program
{
static void Main(string[] args)
{
var allegato = new Root();
var tableType1 = new TableType1();
tableType1.ValueA = "AAA";
allegato.Tables.Add(tableType1);
var tableType2 = new TableType2();
tableType2.ValueB = "BBB";
allegato.Tables.Add(tableType2);
var string2Write = SerializeToString(allegato);
File.WriteAllText(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Test.xml"), string2Write);
}
public static string SerializeToString(T value)
{
var emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
var serializer = new XmlSerializer(value.GetType());
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
using (var stream = new StringWriter())
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, value, emptyNamespaces);
return stream.ToString();
}
}
}
Проблема в том, что я не могу удалить xmlns из XML, вот что я понимаю:
Подробнее здесь: https://stackoverflow.com/questions/792 ... nt-classes
Мобильная версия