向 XML 添加命名空间前缀

发布于 2025-01-09 05:46:11 字数 2037 浏览 0 评论 0原文

我有许多 POCO 类,它们生成序列化所需的 XML 结构,以作为 SOAP WS 中的返回进行传递。在选定的元素中,我需要向元素添加名称空间前缀/别名。这可能吗?如果是的话请问如何?

POCO 类

public partial class ValueTable
{

    private List<ValueTableColumn> headerField;

    private row[] rowField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("column", IsNullable = false)]
    public List<ValueTableColumn> header
    {
        get
        {
            return this.headerField;
        }
        set
        {
            this.headerField = value;
        }
    }


    /// <remarks/>
    [XmlElement]
    public row[] row
    {
        get
        {
            return this.rowField;
        }
        set
        {
            this.rowField = value;
        }
    }

}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class row
{

    private rowField[] fieldField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("field")]
    public rowField[] field
    {
        get
        {
            return this.fieldField;
        }
        set
        {
            this.fieldField = value;
        }
    }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class rowField
{

    private string textField;

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

具有所需名称空间前缀/别名的示例 XML

    <v21:row>
            <v21:field>
                <v21:text>ID-1005896</v21:text>
            </v21:field>
            <v21:field>
                <v21:text>Smith</v21:text>
            </v21:field>
<v21:field>
                <v21:text>64AF6547DDSEE</v21:text>
            </v21:field>
        </v21:row>

目前,返回的内容不带前缀/别名。

我很感激任何帮助:)

I have a number of POCO classes that are generating the required XML structure for serializtion to be passed as return in a SOAP WS. In selected elements I need to add namespace prefix/ alais to the elements. Is this possible? If yes how please?

POCO Classes

public partial class ValueTable
{

    private List<ValueTableColumn> headerField;

    private row[] rowField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("column", IsNullable = false)]
    public List<ValueTableColumn> header
    {
        get
        {
            return this.headerField;
        }
        set
        {
            this.headerField = value;
        }
    }


    /// <remarks/>
    [XmlElement]
    public row[] row
    {
        get
        {
            return this.rowField;
        }
        set
        {
            this.rowField = value;
        }
    }

}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class row
{

    private rowField[] fieldField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("field")]
    public rowField[] field
    {
        get
        {
            return this.fieldField;
        }
        set
        {
            this.fieldField = value;
        }
    }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class rowField
{

    private string textField;

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

Sample XML with required namespace prefixes/alias

    <v21:row>
            <v21:field>
                <v21:text>ID-1005896</v21:text>
            </v21:field>
            <v21:field>
                <v21:text>Smith</v21:text>
            </v21:field>
<v21:field>
                <v21:text>64AF6547DDSEE</v21:text>
            </v21:field>
        </v21:row>

Currently it is being returned with out the prefixes/alias.

I appreciate any help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2025-01-16 05:46:11

像这样的东西应该有效:

using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;

XmlSerializer serializer = new XmlSerializer(typeof(ValueTable));
XmlDocument doc = new XmlDocument();
XPathNavigator xPathNavigator = doc.CreateNavigator();
var nameSpaces = new XmlSerializerNamespaces();
nameSpaces.Add("yourPrefix", "http://your.namespace.com");
using (var xmlWriter = xPathNavigator.AppendChild())
{
    serializer.Serialize(xmlWriter, valueTableObject, nameSpaces);
}

something like this should work:

using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;

XmlSerializer serializer = new XmlSerializer(typeof(ValueTable));
XmlDocument doc = new XmlDocument();
XPathNavigator xPathNavigator = doc.CreateNavigator();
var nameSpaces = new XmlSerializerNamespaces();
nameSpaces.Add("yourPrefix", "http://your.namespace.com");
using (var xmlWriter = xPathNavigator.AppendChild())
{
    serializer.Serialize(xmlWriter, valueTableObject, nameSpaces);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文