C# 当嵌套对象具有名称空间时,如何反序列化XML

C# 当嵌套对象具有名称空间时,如何反序列化XML,c#,xml,deserialization,xml-namespaces,xml-deserialization,C#,Xml,Deserialization,Xml Namespaces,Xml Deserialization,给定以下XML: <webParts> <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"> <title>Title One</title> </webPart> <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"> <title>Title Two</t

给定以下XML:

<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <title>Title One</title>
  </webPart>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <title>Title Two</title>
  </webPart>
</webParts>

标题一
标题二
以及以下c#:

[XmlRoot(“Web部件”)]
公共类Web部件:列表
{
来自XML的静态公共Web部件(字符串路径)
{
webParts returnValue=null;
var serializer=newxmlserializer(typeof(webParts));
使用(var stream=File.OpenRead(path))
{
returnValue=(Web部件)序列化程序。反序列化(流);
}
返回值;
}
}
公共类网页部件
{
公共字符串标题{get;set;}
}
我正在尝试反序列化XML。我无法控制XML,我可以更改c#。如果我删除webPart元素中的名称空间,可以在反序列化过程中执行此操作,效果很好。然而,这似乎有点笨拙。我觉得应该在类中添加XML属性,但找不到名称空间标记的正确组合。上面的代码反序列化Web部件,但计数为0,没有一个Web部件元素被反序列化。为了使这项工作顺利进行,应该对c#采取什么措施?谢谢

使用VS从XML构建类(在大多数情况下)是一项非常简单的任务

  • 将XML字符串复制到剪贴板中
  • 在VS菜单中
您问题中的XML将转换为该结构

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
[System.Xml.Serialization.XmlRootAttribute( Namespace = "", IsNullable = false )]
public partial class webParts
{

    private webPart[ ] webPartField;        

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute( "webPart", Namespace = "http://schemas.microsoft.com/WebPart/v3" )]
    public webPart[ ] webPart
    {
        get 
        {
            return this.webPartField;
        }
        set
        {
            this.webPartField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true, Namespace = "http://schemas.microsoft.com/WebPart/v3" )]
[System.Xml.Serialization.XmlRootAttribute( Namespace = "http://schemas.microsoft.com/WebPart/v3", IsNullable = false )]
public partial class webPart
{

    private string titleField;

    /// <remarks/>
    public string title
    {
        get
        {
            return this.titleField;
        }
        set
        {
            this.titleField = value;
        }
    }
}

那回答使我热泪盈眶。谢谢你,鲁福先生!不知道那是在VS。
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
[System.Xml.Serialization.XmlRootAttribute( Namespace = "", IsNullable = false )]
public partial class webParts
{

    private webPart[ ] webPartField;        

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute( "webPart", Namespace = "http://schemas.microsoft.com/WebPart/v3" )]
    public webPart[ ] webPart
    {
        get 
        {
            return this.webPartField;
        }
        set
        {
            this.webPartField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true, Namespace = "http://schemas.microsoft.com/WebPart/v3" )]
[System.Xml.Serialization.XmlRootAttribute( Namespace = "http://schemas.microsoft.com/WebPart/v3", IsNullable = false )]
public partial class webPart
{

    private string titleField;

    /// <remarks/>
    public string title
    {
        get
        {
            return this.titleField;
        }
        set
        {
            this.titleField = value;
        }
    }
}
public partial class webParts
{
    static public webParts FromXml(string path)
    {
        webParts returnValue = null;
        var serializer = new XmlSerializer(typeof(webParts));
        using (var stream = File.OpenRead(path))
        {
            returnValue = (webParts)serializer.Deserialize(stream);
        }
        return returnValue;
    }
}