0
私はサンプルとして以下のXMLを持っている:私は、XMLをデシリアライズしようとしていると、正常ノードの値を取得することができたのです今逆シリアル化のXMLノードは
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:api="http://www.sample.com/api">
<entry>
<existing>true</existing>
<api:object category="user" id="100">
<api:last-name>Smith</api:last-name>
<api:first-name>John</api:first-name>
</api:object>
</entry>
<entry>
<existing>false</existing>
<api:object category="user" id="101">
<api:last-name>Smith</api:last-name>
<api:first-name>Bob</api:first-name>
</api:object>
</entry>
</feed>
< 既存の>問題は、< api:>という接頭辞を持つノード内のデータのnull値を取得することです。私はこれが名前空間と関係があることを読んでいますが、私はC#MVCを使用しています。以下
マイモデル:
ElementsUsersRoot.cs
[Serializable]
[XmlType(Namespace = "http://www.w3.org/2005/Atom")]
[XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom", IsNullable = false)]
public class ElementsUsersRoot
{
public ElementsUsersRoot()
{
ElementsUsersDetails = new List<ElementsUsersData>();
}
[XmlElement("entry")]
public List<ElementsUsersData> ElementsUsersDetails { get; set; }
ElementsUsersData.cs
[Serializable]
[XmlRoot("entry")]
public class ElementsUsersData
{
[XmlElement("existing")]
public bool IsExisting { get; set; }
[XmlElement("api:object")]
public ElementsUsersAttributes UserAttributes { get; set; }
}
ElementsUsersAttributes.cs
[Serializable]
[XmlRoot("api:object", Namespace = "http://www.sample.com/api")]
public class ElementsUsersAttributes
{
[XmlElement("api:first-name")]
public string Fname { get; set; }
[XmlElement("api:last-name")]
public string Lname{ get; set; }
}
以下のコード:
using (var client = new HttpClient())
{
var byteArray = Encoding.ASCII.GetBytes(apiAccount + ":" + apiPassword);
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Authorization = header;
var stream = await client.GetStreamAsync(apiEndpoint);
var serializer = new XmlSerializer(typeof(ElementsUsersRoot));
ElementsUsersRoot userXmlData = (ElementsUsersRoot)serializer.Deserialize(stream);
//Add cache here
return userXmlData;
}
接頭辞* *名前の「一部」ではありません。例えば。 'http:// www.sample.com/api'名前空間から' object'という名前の要素があります。 * XMLの一部である 'api'が名前空間を表すために使われたという事実は意味がありません*。 –