简体   繁体   English

如何将 XSI 类型添加到使用 LINQ 序列化的元素

[英]How Can I add an XSI type to an element that is being serialized using LINQ

I want to serialize an Object of a Type of a particular class and containing a particular XSI Type我想序列化特定类的类型并包含特定 XSI 类型的对象

Can I Do this in LINQ ?我可以在 LINQ 中执行此操作吗?

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. LINQ 代表Language-Integrated-Query,它是这样的:一种查询技术,允许您将来自各种数据源的数据查询到一个对象结果中。 You want to do something completely different, so LINQ is just the wrong tool.你想做一些完全不同的事情,所以 LINQ 只是错误的工具。

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.另外,我猜用于从示例 XML 文件(或架构)生成类的 xsd.exe 不会对您有太大帮助,因为它不够聪明,无法检测架构类之间的继承。

Therefore, I would recommend to write an XML schema manually and then use xsd.exe to generate classes for that schema.因此,我建议手动编写 XML 模式,然后使用 xsd.exe 为该模式生成类。 You could then instantiate those classes and the XmlSerializer will give you an output as you expect.然后,您可以实例化这些类, XmlSerializer会按照您的预期为您提供输出。

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 ).模式应该有点像下面的(我在这里排除了实际的模型内容,你必须选择是把它放在Model还是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>

To get the following XML:要获取以下 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. LINQ to XML 是一个令人沮丧的练习。 In the long term you may prefer to use concrete classes with appropriate XML serialization decorators.从长远来看,您可能更喜欢使用带有适当 XML 序列化装饰器的具体类。

=== edit === === 编辑 ===

Here is some additional code that writes the data to an XML file:下面是一些将数据写入 XML 文件的附加代码:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM