简体   繁体   English

(可选)在反序列化时从xml省略名称空间?

[英]Optionally omit namespace from xml on deserialization?

I want the option to omit the xmlns:xsi, xmlns:xsd and xmlns attributes from xml content. 我希望该选项从xml内容中省略xmlns:xsi,xmlns:xsd和xmlns属性。 When doing so deserialization is failing. 这样做时反序列化失败。

This is the xsd definition: 这是xsd定义:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="CSVDataPluginConfig"
    targetNamespace="http://tempuri.org/CSVDataPluginConfig.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/CSVDataPluginConfig.xsd"
    xmlns:mstns="http://tempuri.org/CSVDataPluginConfig.xsd"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
  <xsd:element name="CSVDataPluginConfig" type="CSVDataPluginConfig"/>
  <xsd:complexType name="CSVDataPluginConfig">
  ...
  </xsd:complexType>
</xsd:schema>

The xsd.exe code generator is giving this: xsd.exe代码生成器提供了以下功能:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(
    Namespace="http://tempuri.org/CSVDataPluginConfig.xsd")]
[System.Xml.Serialization.XmlRootAttribute(
    Namespace="http://tempuri.org/CSVDataPluginConfin.xsd", IsNullable=false)]
public partial class CSVDataPluginConfig {
}

This is the sample of xml content that deserializes successfully: 这是成功反序列化的xml内容示例:

<?xml version="1.0" encoding="utf-16"?>
<CSVDataPluginConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://tempuri.org/CSVDataPluginConfig.xsd">
...
</CSVDataPluginConfig>

For simplicity and making it easier to handwrite the xml, I want to be able to successfully deserialize the following: 为了简单起见,并使其更容易手写xml,我希望能够成功反序列化以下内容:

<CSVDataPluginConfig>
...
</CSVDataPluginConfig>

I am deserializing using this extension method: 我正在使用此扩展方法反序列化:

    public static T DeserializeXML<T>(this string xml)
    {
        T obj;
        using (StringReader reader = new StringReader(xml))
        {
            obj = (T)new XmlSerializer(typeof(T)).Deserialize(reader);
            reader.Close();
        }
        return obj;
    }

Using Visual Studio 2008, what are my options, and what is the best option? 使用Visual Studio 2008,我有哪些选择,最好的选择是什么?

Sorry, you don't actually have an option. 抱歉,您实际上没有选择。

The author of the XML is (properly) using namespaces. XML的作者(正确地)使用名称空间。 That means the XML is in a namespace, and you have to use it. 这意味着XML在命名空间中,您必须使用它。

In fact, with a good XML Editor, the namespaces make it easier to hand-enter it. 实际上,有了一个好的XML编辑器,命名空间使手动输入变得更加容易 The namespaces map to a schema, and the schema can inform the XML editor how to help you with the data entry. 名称空间映射到模式,该模式可以通知XML编辑器如何帮助您进行数据输入。 Try the XML editor in Visual Studio 2010 and you'll see what I mean. 在Visual Studio 2010中尝试XML编辑器,您将明白我的意思。 Just make sure that the schema is available (maybe in the same project), and it will go as far as filling in the XML with placeholders as soon as you type <CSV TAB 只需确保该模式可用(也许在同一项目中),并且只要键入<CSV TAB ,它就可以使用占位符填充XML。

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

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