gpt4 book ai didi

c# - 删除命名空间标记 (xmlns :) from XMLSerializer

转载 作者:行者123 更新时间:2023-11-30 21:10:14 26 4
gpt4 key购买 nike

我想在我的 C# 代码中生成以下 xml 输出:

<?xml version="1.0" encoding="utf-16"?>
<CallConnectReq Xmlns="urn:interno-com:ns:a9c" reqId="9" msgNb="2">
<LocalCallId>0</LocalCallId>
</CallConnectReq>

现在我正在实现如下:

var xnameSpace = new XmlSerializerNamespaces();
xnameSpace.Add("Xmlns", Constants.XmlNameSpaceValue);
var xmlSerializer = new XmlSerializer(objToSerialize.GetType());
var stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, objToSerialize, xnameSpace);
return stringWriter.ToString().**Replace("xmlns:","");**

但我想在不使用 Replace() 方法的情况下删除“xmlns:” 标记。有什么办法吗?

最佳答案

添加只是默认命名空间:

var xnameSpace = new XmlSerializerNamespaces();
xnameSpace.Add("", "urn:interno-com:ns:a9c");
var ser = new XmlSerializer(typeof (CallConnectRequest));
ser.Serialize(destination, new CallConnectRequest(), xnameSpace);

与:

[XmlRoot("CallConnectReq", Namespace = "urn:interno-com:ns:a9c")]
public class CallConnectRequest {}

关于c# - 删除命名空间标记 (xmlns :) from XMLSerializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8600658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com