请建议我使用c#将xml字符串反序列化为数据表的任何解决方案。我没有任何相关的解决方案。提前谢谢。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<seatMap>
<errorCode>0</errorCode>
<xml>
<Seat-Map>
<Lower-Deck>
<column id="1">
<row id="1"><seat number="1C" status="0" /> </row>
<row id="2"><seat number="2C" status="0" /> </row>
<row id="3"><seat number="3C" status="0" /> </row>
<row id="4"><seat number="4C" status="0" /> </row>
<row id="5"><seat number="5C" status="0" /> </row>
<row id="6"><seat number="6C" status="0" /> </row>
<row id="7"><seat number="7C" status="0" /> </row>
<row id="8"><seat number="8C" status="0" /> </row>
<row id="9"><seat number="9C" status="0" /> </row>
<row id="10"><seat number="10C" status="0" /> </row>
</column>
<column id="2">
<row id="1"><seat number="1B" status="0" /> </row>
<row id="2"><seat number="2B" status="0" /> </row>
<row id="3"><seat number="3B" status="0" /> </row>
<row id="4"><seat number="4B" status="0" /> </row>
<row id="5"><seat number="5B" status="0" /> </row>
<row id="6"><seat number="6B" status="0" /> </row>
<row id="7"><seat number="7B" status="0" /> </row>
<row id="8"><seat number="8B" status="0" /> </row>
<row id="9"><seat number="9B" status="0" /> </row>
<row id="10"><seat number="10B" status="0" /> </row>
</column>
<column id="3">
<row id="1"><seat number="#" status="blank"/> </row>
<row id="2"><seat number="#" status="blank"/> </row>
<row id="3"><seat number="#" status="blank"/> </row>
<row id="4"><seat number="#" status="blank"/> </row>
<row id="5"><seat number="#" status="blank"/> </row>
<row id="6"><seat number="#" status="blank"/> </row>
<row id="7"><seat number="#" status="blank"/> </row>
<row id="8"><seat number="#" status="blank"/> </row>
<row id="9"><seat number="#" status="blank"/> </row>
<row id="10"><seat number="#" status="blank"/> </row>
</column>
<column id="4">
<row id="1"><seat number="1A" status="0" /> </row>
<row id="2"><seat number="2A" status="0" /> </row>
<row id="3"><seat number="3A" status="0" /> </row>
<row id="4"><seat number="4A" status="0" /> </row>
<row id="5"><seat number="5A" status="0" /> </row>
<row id="6"><seat number="6A" status="0" /> </row>
<row id="7"><seat number="7A" status="0" /> </row>
<row id="8"><seat number="8A" status="0" /> </row>
<row id="9"><seat number="9A" status="0" /> </row>
<row id="10"><seat number="10A" status="0" /> </row>
</column>
</Lower-Deck>
</Seat-Map>
</xml>
</seatMap>
但我需要从XML字符串中跟踪DataTable:
public static DataTable querySeat(string bus_id)
{
// Query the Seat Layout of the Trip
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn("column", Type.GetType("System.Int32")); // Column No. of the seat
dt.Columns.Add(dc);
dc = new DataColumn("row", Type.GetType("System.Int32")); // Row N. of the seat
dt.Columns.Add(dc);
dt.Columns.Add("seat"); // Seat Label
dc = new DataColumn("deck", Type.GetType("System.Int32")); // Deck, 1 = Lower Deck, 2 = Upper Deck
dt.Columns.Add(dc);
dt.Columns.Add("status"); // Status, 1 = Available, 0 = Taken
return dt;
}
DataTable输出:
Coulmn | Row | Seat | Deck | Status
--------------------------------------------------------------------------
1 1 1c 1 0
1 2 2c 1 0
1 3 3c 1 0
答案 0 :(得分:0)
使用DataTable对象的ReadXml方法。
答案 1 :(得分:0)
您可以使用数据集的WriteXML和ReadXML方法将DataSet转换为XML并返回。 (例如,检查Convert Dataset to XML)
只有在您自己从应用程序创建XML时才可以这样做。
答案 2 :(得分:0)
检查以下代码
public static DataTable querySeat(string id)
{
string xmlStr = ""; //Place Your XML String here
DataTable result = new DataTable("SeatData");
DataColumn columnColumn = result.Columns.Add("column", typeof(Int32));
DataColumn rowColumn = result.Columns.Add("row", typeof(Int32));
DataColumn seatColumn = result.Columns.Add("seat", typeof(String));
DataColumn deckColumn = result.Columns.Add("deck", typeof(Int32));
DataColumn statusColumn = result.Columns.Add("status", typeof(Int32));
XElement.Parse(HttpUtility.HtmlDecode(xmlStr)).Descendants("seat").ToList().ForEach(
(element) =>
{
DataRow row = result.NewRow();
row.SetField<int>(columnColumn, Convert.ToInt32(element.Parent.Parent.Attribute("id").Value));
row.SetField<int>(rowColumn, Convert.ToInt32(element.Parent.Attribute("id").Value));
row.SetField<string>(seatColumn, element.Attribute("number").Value);
int deckNo = -1;
if (element.Parent.Parent.Parent.Name.LocalName == "Lower-Deck")
deckNo = 1;
row.SetField<int>(deckColumn, deckNo);
string statusString = element.Attribute("status").Value;
int statusValue = -1;
if (statusString == "blank")
statusValue = 1;
else if (statusString == "0")
statusValue = 0;
row.SetField<int>(statusColumn, statusValue);
result.Rows.Add(row);
}
);
return result;
}