我需要查询SQLCE表并将结果集转换为XML。数据库的列:
line_id int
ref_no nvarchar(20)
. . .
new_item int
...与所需的XML进行1:1映射:
<INV>
<line_id>1</line_id>
<ref_no>valerie</ref_no>
. . .
<new_item>-1</new_item>
</INV>
我可以生成这样的XML(伪代码):
int lineId;
String refNum;
. . .
String strXML;
StringBuilder sbXML;
. . . // loop through result set
lineId = resultSet[0];
refNum = resultSet[1];
. . .
strXML = String.Format("<INV><line_id>{0}</line_id><ref_no>{1}</ref_no>. . .<new_item>{2}</new_item></INV>", lineId, refNum, . . .newItem);
sbXML.Add(strXML);
......但这看起来很笨拙。有更优雅的解决方案吗?
我使用了ctacke提供的链接中的第一个例子,现在我有:
. . .
string xmlOutput = String.Empty;
. . .
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("Command");
doc.AppendChild(root);
try
{
while (dtr.Read())
{
// outer INV
XmlElement invRec = doc.CreateElement("INV");
// Line ID
lineId = dtr["line_id"].ToString(); // Wiggly uses dtr.GetString(1); would then have to use GetX()...
XmlElement _lineId = doc.CreateElement("line_id");
_lineId.InnerText = lineId;
invRec.AppendChild(_lineId);
// Ref Num
refNum = dtr["ref_no"].ToString();
XmlElement _refNum = doc.CreateElement("ref_no");
_refNum.InnerText = refNum;
invRec.AppendChild(_refNum);
. . .
root.AppendChild(invRec);
}
}
finally
{
//doc.AppendChild(root); <= Should this be here instead of above?
xmlOutput = doc.OuterXml;
}
. . .
或者,对于可以在没有任何设置的情况下提升和丢弃/填充的测试代码:
private void button29_Click(object sender, EventArgs e)
{
string xmlOutput = String.Empty;
String lineId;
String refNum;
String upcCode;
String desc;
String dept;
String vendorId;
String upcPackSize;
String Id;
String packSize;
String unitCost;
String unitList;
String unitQty;
String newItem;
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("Command");
doc.AppendChild(root);
try
{
// outer INV
XmlElement invRec = doc.CreateElement("INV");
// Line ID
lineId = "some line id";
XmlElement _lineId = doc.CreateElement("line_id");
_lineId.InnerText = lineId;
invRec.AppendChild(_lineId);
// Ref Num
refNum = "some ref num";
XmlElement _refNum = doc.CreateElement("ref_no");
_refNum.InnerText = refNum;
invRec.AppendChild(_refNum);
// UPC Code
upcCode = "some upc code";
XmlElement _upc_code = doc.CreateElement("upc_code");
_upc_code.InnerText = upcCode;
invRec.AppendChild(_upc_code);
// Description
desc = "some desc";
XmlElement _description = doc.CreateElement("description");
_description.InnerText = desc;
invRec.AppendChild(_description);
// Department
dept = "some dept";
XmlElement _department = doc.CreateElement("department");
_department.InnerText = dept;
invRec.AppendChild(_department);
// Vendor Id
vendorId = "some vendor id";
XmlElement _vendor_id = doc.CreateElement("vendor_id");
_vendor_id.InnerText = vendorId;
invRec.AppendChild(_vendor_id);
// UPC Pack Size
upcPackSize = "some upc pack size";
XmlElement _upc_pack_size = doc.CreateElement("upc_pack_size");
_upc_pack_size.InnerText = upcPackSize;
invRec.AppendChild(_upc_pack_size);
// Id
Id = "some id";
XmlElement _id = doc.CreateElement("id");
_id.InnerText = Id;
invRec.AppendChild(_id);
// Pack Size
packSize = "some pack size";
XmlElement _pack_size = doc.CreateElement("pack_size");
_pack_size.InnerText = packSize;
invRec.AppendChild(_pack_size);
// Unit Cost
unitCost = "some unit cost";
XmlElement _unit_cost = doc.CreateElement("unit_cost");
_unit_cost.InnerText = unitCost;
invRec.AppendChild(_unit_cost);
// Unit List
unitList = "some unit list";
XmlElement _unit_list = doc.CreateElement("unit_list");
_unit_list.InnerText = unitList;
invRec.AppendChild(_unit_list);
// Unit Qty
unitQty = "some unit qty";
XmlElement _unit_qty = doc.CreateElement("unit_qty");
_unit_qty.InnerText = unitQty;
invRec.AppendChild(_unit_qty);
// New Item
newItem = "some New item";
XmlElement _new_item = doc.CreateElement("new_item");
_new_item.InnerText = newItem;
invRec.AppendChild(_new_item);
root.AppendChild(invRec);
}
finally
{
xmlOutput = doc.OuterXml;
MessageBox.Show(xmlOutput);
}
}
答案 0 :(得分:1)
你肯定不想要使用字符串格式。既然您已经降级到.NET 1.0(从未有过.NETCF 1.1),那么您将使用XmlDocument
类作为起点,并使用它来创建随后添加到文档中的节点和属性。与LINQ的XDocument
相比,它非常难看,但它是你拥有的工具。
快速搜索turned up this example,这是一本很好的入门书。