有没有办法使用IXmlSerializable重写这段代码,或者更好的是,通过自定义属性?
// TODO: how to harvest the namespaces from the request using a model and XmlSerializer?
var xmlStr = await Helper.Post (uri, request);
var xml = XDocument.Parse (xmlStr);
var node = (XElement) xml.FirstNode;
var ns = Namespaces = new XmlSerializerNamespaces ();
if (node.HasAttributes)
{
foreach (var attr in node.Attributes ())
{
if (attr.IsNamespaceDeclaration)
{
ns.Add (attr.Name.LocalName, attr.Value);
}
}
}
我问,因为我想将xml反序列化为模型,我希望模型包含响应中所做的所有命名空间声明,但我不知道它们是什么。这是一个OGC GetCaps响应,OGC使用命名空间来识别在设计时未知的服务。
以下是更全面的相同代码:
public async Task<Loader> Initialize (Uri uri)
{
var request = new GetCapabilities.GetCapabilitiesRequest ();
// TODO: use another form of caching (or allow support for expiration)
var response = Helper.CachedGetter.Post<GetCapabilities.GetCapabilitiesResponse> (uri, request);
await response;
GetCaps = response.Result;
// TODO: need to harvest the namespaces from the request but the model can't do it.
{
var xmlStr = await Helper.Post (uri, request);
var xml = XDocument.Parse (xmlStr);
var node = (XElement) xml.FirstNode;
var ns = Namespaces = new XmlSerializerNamespaces ();
if (node.HasAttributes)
{
foreach (var attr in node.Attributes ())
{
if (attr.IsNamespaceDeclaration)
{
ns.Add (attr.Name.LocalName, attr.Value);
}
}
}
}
return this;
}