【发布时间】:2015-04-14 23:06:13
【问题描述】:
我正在重做 MVC 中的旧版 VB.Net 应用程序。该应用程序的一部分使用 MSXML2 创建 XML 文档。到目前为止,我在两个元素方面遇到了麻烦。遗留代码使用“Right”(Right Function in C#?)和“Format”(Equivalent of Format of VB in C#)命令。
除了使用 System.XML 之外,还有其他创建 XML 的首选方法吗?
objNode = objDoc.CreateNode("element", "Payout", "");
objElement = objDoc.DocumentElement.AppendChild(objNode);
objElement.SetAttribute("companyNumber", Right(objRs.Fields["operating_unit_code"].Value, 3));
objElement.SetAttribute("employeeNumber", objRs.Fields["employee_code"].Value);
objElement.SetAttribute("earningsCode", objRs.Fields["incentive_type_code"].Value);
objElement.SetAttribute("chargeToProject", Right(objRs.Fields["operating_unit_code"].Value, 3) & objRs.Fields["organization_code"].Value);
objElement.SetAttribute("amount", Format(objRs.Fields["sum_calc_amount"].Value, "#.00"));
objElement.SetAttribute("weekEndingDate", Format(objRs.Fields["pay_period_end_date"].Value, "MM/DD/YYYY"));
objElement.SetAttribute("employeeName", objRs.Fields["full_name"].Value);
【问题讨论】:
-
这两个问题还差得远?
-
请查看我的编辑。如果这不符合您的意图 - 请确保清楚您所知道的以及您需要帮助的内容,并相应地更新问题。
-
我正准备推荐 XML Literals,但后来意识到您要从 vb.net 转到 C#。仍然会建议
XDocument和/或XElement。你希望你的 XML 输出是什么样的? -
@AlexeiLevenkov - 您的编辑是正确的。
-
@CoderDennis - 一个标准的 XML 文档。不确定是否使用 StringBuilder 来创建它。