<response xmlns=''> 不是预期的
<response xmlns=''> was not expected
我正在将 BigBlueButton API 与 Asp Net MVC 集成。但是我无法从 api 读取响应 xml 文件。
这是一个力模块项目。
我的代码:
public ActionResult GetMeetings()
{
List<AllMeetings> allMeetings = new List<AllMeetings>();
XmlSerializer serializer = new XmlSerializer(typeof(List<AllMeetings>), "response");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://bbb.hsu.ac.ir/bigbluebutton/api/getMeetings?checksum=aca692f682f06312cb43f14564ddf96cb76925ed");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader sr = new StreamReader(dataStream);
List<AllMeetings> allMeeting =(List<AllMeetings>)serializer.Deserialize(sr);
return View(allMeeting);
}
我编写了一个 class 以便我可以将属性定义为 xml 元素并将它们用作可枚举模型
namespace BBBJavaScriptTest
{
[Serializable]
[XmlType("response")]
public class AllMeetings
{
[XmlElement(ElementName = "meetingName")]
public string meetingName { get; set; }
[XmlElement(ElementName = "meetingID")]
public string meetingID { get; set; }
[XmlElement(ElementName = "createDate")]
public string createDate { get; set; }
[XmlElement(ElementName = "voiceBridge")]
public string voiceBridge { get; set; }
[XmlElement(ElementName = "moderatorPW")]
public string moderatorPW { get; set; }
public AllMeetings()
{
}
}
并且视图部分是 Razor 视图:
@model IEnumerable<AllMeetings>
@{
ViewBag.Title = "GetMeetings";
Layout = null;
}
<h2>GetMeetings</h2>
<table class="table table-bordered">
<tr>
<td>meetingID</td>
<td>meetingName</td>
<td>createDate</td>
<td>voiceBridge</td>
<td>moderatorPW</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.meetingID</td>
<td>@item.meetingName</td>
<td>@item.createDate</td>
<td>@item.voiceBridge</td>
<td>@item.moderatorPW</td>
</tr>
}
</table>
响应样本:
<response>
<returncode>SUCCESS</returncode>
<meetings>
<meeting>
<meetingName>****</meetingName>
<meetingID>****</meetingID>
<internalMeetingID>
****
</internalMeetingID>
<createTime>1603543982183</createTime>
<createDate>Sat Oct 24 16:23:02 IRST 2020</createDate>
<voiceBridge>78409</voiceBridge>
<dialNumber>****</dialNumber>
<attendeePW>ap</attendeePW>
<moderatorPW>mp</moderatorPW>
<running>true</running>
<duration>400</duration>
<hasUserJoined>true</hasUserJoined>
<recording>false</recording>
<hasBeenForciblyEnded>false</hasBeenForciblyEnded>
<startTime>1603543982435</startTime>
<endTime>0</endTime>
<participantCount>1</participantCount>
<listenerCount>1</listenerCount>
<voiceParticipantCount>0</voiceParticipantCount>
<videoCount>0</videoCount>
<maxUsers>0</maxUsers>
<moderatorCount>1</moderatorCount>
<attendees>
<attendee>
<userID>****</userID>
<fullName>****</fullName>
<role>MODERATOR</role>
<isPresenter>true</isPresenter>
<isListeningOnly>true</isListeningOnly>
<hasJoinedVoice>false</hasJoinedVoice>
<hasVideo>false</hasVideo>
<clientType>HTML5</clientType>
</attendee>
</attendees>
<metadata> </metadata>
<isBreakout>false</isBreakout>
</meeting>
</meetings>
</response>
我解决了这个问题,这里是解决方案:
XDocument doc = XDocument.Load(sr);
List<AllMeetings> meetings = new List<AllMeetings>();
foreach (XElement element in doc.Element("response").Elements("meetings").Elements("meeting"))
{
AllMeetings newMeeting = new AllMeetings();
newMeeting.meetingName = (string)element.Element("meetingName");
meetings.Add(newMeeting);
}
return View(meetings);
我正在将 BigBlueButton API 与 Asp Net MVC 集成。但是我无法从 api 读取响应 xml 文件。 这是一个力模块项目。
我的代码:
public ActionResult GetMeetings()
{
List<AllMeetings> allMeetings = new List<AllMeetings>();
XmlSerializer serializer = new XmlSerializer(typeof(List<AllMeetings>), "response");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://bbb.hsu.ac.ir/bigbluebutton/api/getMeetings?checksum=aca692f682f06312cb43f14564ddf96cb76925ed");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader sr = new StreamReader(dataStream);
List<AllMeetings> allMeeting =(List<AllMeetings>)serializer.Deserialize(sr);
return View(allMeeting);
}
我编写了一个 class 以便我可以将属性定义为 xml 元素并将它们用作可枚举模型
namespace BBBJavaScriptTest
{
[Serializable]
[XmlType("response")]
public class AllMeetings
{
[XmlElement(ElementName = "meetingName")]
public string meetingName { get; set; }
[XmlElement(ElementName = "meetingID")]
public string meetingID { get; set; }
[XmlElement(ElementName = "createDate")]
public string createDate { get; set; }
[XmlElement(ElementName = "voiceBridge")]
public string voiceBridge { get; set; }
[XmlElement(ElementName = "moderatorPW")]
public string moderatorPW { get; set; }
public AllMeetings()
{
}
}
并且视图部分是 Razor 视图:
@model IEnumerable<AllMeetings>
@{
ViewBag.Title = "GetMeetings";
Layout = null;
}
<h2>GetMeetings</h2>
<table class="table table-bordered">
<tr>
<td>meetingID</td>
<td>meetingName</td>
<td>createDate</td>
<td>voiceBridge</td>
<td>moderatorPW</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.meetingID</td>
<td>@item.meetingName</td>
<td>@item.createDate</td>
<td>@item.voiceBridge</td>
<td>@item.moderatorPW</td>
</tr>
}
</table>
响应样本:
<response>
<returncode>SUCCESS</returncode>
<meetings>
<meeting>
<meetingName>****</meetingName>
<meetingID>****</meetingID>
<internalMeetingID>
****
</internalMeetingID>
<createTime>1603543982183</createTime>
<createDate>Sat Oct 24 16:23:02 IRST 2020</createDate>
<voiceBridge>78409</voiceBridge>
<dialNumber>****</dialNumber>
<attendeePW>ap</attendeePW>
<moderatorPW>mp</moderatorPW>
<running>true</running>
<duration>400</duration>
<hasUserJoined>true</hasUserJoined>
<recording>false</recording>
<hasBeenForciblyEnded>false</hasBeenForciblyEnded>
<startTime>1603543982435</startTime>
<endTime>0</endTime>
<participantCount>1</participantCount>
<listenerCount>1</listenerCount>
<voiceParticipantCount>0</voiceParticipantCount>
<videoCount>0</videoCount>
<maxUsers>0</maxUsers>
<moderatorCount>1</moderatorCount>
<attendees>
<attendee>
<userID>****</userID>
<fullName>****</fullName>
<role>MODERATOR</role>
<isPresenter>true</isPresenter>
<isListeningOnly>true</isListeningOnly>
<hasJoinedVoice>false</hasJoinedVoice>
<hasVideo>false</hasVideo>
<clientType>HTML5</clientType>
</attendee>
</attendees>
<metadata> </metadata>
<isBreakout>false</isBreakout>
</meeting>
</meetings>
</response>
我解决了这个问题,这里是解决方案:
XDocument doc = XDocument.Load(sr);
List<AllMeetings> meetings = new List<AllMeetings>();
foreach (XElement element in doc.Element("response").Elements("meetings").Elements("meeting"))
{
AllMeetings newMeeting = new AllMeetings();
newMeeting.meetingName = (string)element.Element("meetingName");
meetings.Add(newMeeting);
}
return View(meetings);