나는 관련없는 주제로 지정된 많은 팁과 트릭을 성공 리에 시험해 보았습니다.XML 응답을 C# 개체로 변환 중 - 오류 (xmlns = ''>이 (가) 예상하지 않음)
나는 XML 문자열을 C# 클래스 개체로 deserialise 할 수 없다. <DepartureBoard xmlns=''> was not expected.
"Departure"자식 요소를 특성이있는 개체로 가져 와서 목록에 개체를 추가하려고합니다.
이
는 API에서 XML 데이터의 응답이다 (이 짧게) :public async Task<List<DepartureBoard>> GetDepartureBoard()
{
string url = "theApiUrlHere";
HttpClient client = new HttpClient();
string token = await oauth.RefreshToken();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await client.GetAsync(url);
HttpContent content = response.Content;
HttpContentHeaders headers = response.Content.Headers;
byte[] mycontent = await response.Content.ReadAsByteArrayAsync();
string xmlString = Encoding.Default.GetString(mycontent);
StringReader reader = new StringReader(xmlString);
List<DepartureBoard> result;
XmlSerializer xs = new XmlSerializer(typeof(List<DepartureBoard>), new XmlRootAttribute("Departure"));
result = (List<DepartureBoard>)xs.Deserialize(reader);
return result;
}
는 C# 클래스했다 :
<?xml version="1.0" encoding="UTF-8"?>
<DepartureBoard xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.vasttrafik.se/v1/hafasRestDepartureBoard.xsd" servertime="01:52" serverdate="2017-04-09">
<Departure name="SJ REGIONAL" sname="TÅG" type="VAS" stopid="9022014081600002" stop="Trollhättan station, Trollhättan" time="14:07" date="2017-04-09" journeyid="9015074172200363" direction="Göteborg" track="2" rtTime="14:07" rtDate="2017-04-09" fgColor="#00A5DC" bgColor="#ffffff" stroke="Solid">
<JourneyDetailRef ref="longUrlHere" />
</Departure>
<Departure name="VÄSTTÅGEN" sname="TÅG" type="VAS" stopid="9022014081600002" stop="Trollhättan station, Trollhättan" time="14:22" date="2017-04-09" journeyid="9015014172103251" direction="Göteborg" track="2" rtTime="14:22" rtDate="2017-04-09" fgColor="#00A5DC" bgColor="#ffffff" stroke="Solid">
<JourneyDetailRef ref="longUrlHere" />
</Departure>
</DepartureBoard>
이 내가 변환을 시도하는 방법입니다 "붙여 넣기"를 사용하여 생성됩니다.
using System;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Data.SqlTypes;
/// <remarks/>
[SerializableAttribute()]
[DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
/* I've tried adding both "http://www.w3.org/2001/XMLSchema-instance"
and "http://api.vasttrafik.se/v1/hafasRestDepartureBoard.xsd" to
the namespace attribute below without success.
It was left empty by default. */
[XmlRoot(Namespace = "", IsNullable = false)]
public partial class DepartureBoard
{
private DepartureBoardDeparture[] departureField;
private string servertimeField;
private System.DateTime serverdateField;
/// <remarks/>
[XmlElementAttribute("Departure")]
public DepartureBoardDeparture[] Departure
{
get
{
return this.departureField;
}
set
{
this.departureField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string servertime
{
get
{
return this.servertimeField;
}
set
{
this.servertimeField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute(DataType = "date")]
public System.DateTime serverdate
{
get
{
return this.serverdateField;
}
set
{
this.serverdateField = value;
}
}
}
/// <remarks/>
[SerializableAttribute()]
[DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
public partial class DepartureBoardDeparture
{
private DepartureBoardDepartureJourneyDetailRef journeyDetailRefField;
private string nameField;
private string snameField;
private string typeField;
private ulong stopidField;
private string stopField;
private string timeField;
private System.DateTime dateField;
private ulong journeyidField;
private string directionField;
private byte trackField;
private string rtTimeField;
private System.DateTime rtDateField;
private bool rtDateFieldSpecified;
private string fgColorField;
private string bgColorField;
private string strokeField;
/// <remarks/>
public DepartureBoardDepartureJourneyDetailRef JourneyDetailRef
{
get
{
return this.journeyDetailRefField;
}
set
{
this.journeyDetailRefField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string sname
{
get
{
return this.snameField;
}
set
{
this.snameField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public ulong stopid
{
get
{
return this.stopidField;
}
set
{
this.stopidField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string stop
{
get
{
return this.stopField;
}
set
{
this.stopField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string time
{
get
{
return this.timeField;
}
set
{
this.timeField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute(DataType = "date")]
public System.DateTime date
{
get
{
return this.dateField;
}
set
{
this.dateField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public ulong journeyid
{
get
{
return this.journeyidField;
}
set
{
this.journeyidField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string direction
{
get
{
return this.directionField;
}
set
{
this.directionField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public byte track
{
get
{
return this.trackField;
}
set
{
this.trackField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string rtTime
{
get
{
return this.rtTimeField;
}
set
{
this.rtTimeField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute(DataType = "date")]
public System.DateTime rtDate
{
get
{
return this.rtDateField;
}
set
{
this.rtDateField = value;
}
}
/// <remarks/>
[XmlIgnoreAttribute()]
public bool rtDateSpecified
{
get
{
return this.rtDateFieldSpecified;
}
set
{
this.rtDateFieldSpecified = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string fgColor
{
get
{
return this.fgColorField;
}
set
{
this.fgColorField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string bgColor
{
get
{
return this.bgColorField;
}
set
{
this.bgColorField = value;
}
}
/// <remarks/>
[XmlAttributeAttribute()]
public string stroke
{
get
{
return this.strokeField;
}
set
{
this.strokeField = value;
}
}
}
/// <remarks/>
[SerializableAttribute()]
[DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
public partial class DepartureBoardDepartureJourneyDetailRef
{
private string refField;
/// <remarks/>
[XmlAttributeAttribute()]
public string @ref
{
get
{
return this.refField;
}
set
{
this.refField = value;
}
}
}
응답에서
이
감사를 mkysoft 편집, 나는 지금 조금 더 잘 이해 생각한다. List에서 DepartureBoard-object로 변경하면 오류가 사라집니다. 객체가 Departure 배열을 null로 반환하지만. DepartureBoard-object 내부의 "Departure"배열에 어떻게 액세스 할 수 있습니까? 목록을 시작할 수 없습니다
최저/ J
알아 냈습니다. 루트 요소에서 namespace-attribute를 삭제하면 departure-array가 채워집니다. 그게 문제를 푸는 좋은 방법인지는 모르겠지만 적어도 지금 배열을 얻는다. – jeth0010
당신은 옳은 길에 있습니다. XML 파일에 네임 스페이스가 없습니다. Xml 파일에는 namespace에 대한 xmlns = "xxx"속성이 필요합니다. – mkysoft