1

I want to serialize an Object of a Type of a particular class and containing a particular XSI Type

Can I Do this in LINQ ?

  • this may help: https://learn.microsoft.com/en-us/dotnet/standard/serialization/serialization-how-to-topics – jazb Aug 06 '19 at 05:40
  • the link I provided is intended to help you help yourself to understand and gain some knowledge in the area of serialization – jazb Aug 06 '19 at 05:45

2 Answers2

0

LINQ stands for Language-Integrated-Query and that is what it is: a query technology that allows you to query data from a variety of data sources into an object result. You want to do something completely different, so LINQ is just the wrong tool.

Also, I guess the xsd.exe that is used to generate classes from an example XML file (or schema) won't help you very much because afaik it is not clever enough to detect inheritance between schema classes.

Therefore, I would recommend to write an XML schema manually and then use xsd.exe to generate classes for that schema. You could then instantiate those classes and the XmlSerializer will give you an output as you expect.

The schema should somewhat like the following (I excluded the actual model content here, you have to choose whether to put that in Model or SettingsModel).

<xs:element name="Model" type="Model" />
<xs:class name="Model" abstract="True">
  <xs:complexContent />
</xs:class>
<xs:class name="SettingsModel">
  <xs:complexContent>
    <xs:extension base="Model" />
  </xs:complexContent>
</xs:class>
Georg
  • 5,626
  • 1
  • 23
  • 44
0

To get the following XML:

<Model xsi:type="SettingsModel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Name>Test05</Name>
  <IsActive>false</IsActive>
  <IsHidden>false</IsHidden>
</Model>

You can use the following code:

var model = new { Name = "Test05", IsActive = false, IsHidden = false };

var namespaceName = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsi = XNamespace.Get(namespaceName);
var x = new XElement("Model",
    new XAttribute(xsi + "type", "SettingsModel"),
    new XAttribute(XNamespace.Xmlns + "xsi", namespaceName),
    new XElement("Name", model.Name),
    new XElement("IsActive", model.IsActive),
    new XElement("IsHidden", model.IsHidden)
    );

Console.WriteLine(x);

LINQ to XML is an exercise in frustration. In the long term you may prefer to use concrete classes with appropriate XML serialization decorators.

=== edit ===

Here is some additional code that writes the data to an XML file:

var settings = new XmlWriterSettings()
{
    Indent = true,
    OmitXmlDeclaration = true
};
using (var stream = new FileStream("Test05.xml", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
using (var writer = XmlWriter.Create(stream, settings))
{
    x.WriteTo(writer);
}
AlwaysLearning
  • 7,915
  • 5
  • 27
  • 35
  • This is a really Great answer , Thank you very much I do wonder tho the data that gets serilized does get serialized real time but once the application is closed it re modifies and the data added gets erased off any Idea why that might be ? –  Aug 07 '19 at 11:15
  • I'm not sure what you're asking. `Console.WriteLine()` is only printing it to the application's console window and, when running it from Visual Studio, that window closes as soon as the application exits. If you're wanting to persist the data to a file you'll need to write it to a `FileStream` object instead of the console. – AlwaysLearning Aug 07 '19 at 11:20
  • my bad I forgot to tell you that I,m writing it to a separate xml file instead of printing it on the console –  Aug 07 '19 at 11:30