我得到了一个 xsd 生成的 C# POCO 对象,我需要将其转换为 xml。然而,预期的有效载荷与我得到的 xsds 不匹配。具体来说,我需要省略声明并从 xml 对象中删除所有命名空间,以便相关公司接受 API 请求。
问题
给定一个类型为 T 的对象,我想在没有声明和命名空间的情况下对其进行序列化。
我已经摆脱了大部分,但由于某种原因,q1 已添加到每个元素中。我如何删除它?
试图
经过一些研究,我看到一些帖子提供了一个解决方案,该解决方案创建一个空的 xml 序列化器命名空间并使用该对象调用序列化器。那只让我成功了一半。
用法
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
var body = payload.SerializeObject(false, true, ns);
Run Code Online (Sandbox Code Playgroud)
扩展方法
public static string SerializeObject<T>(this T obj, bool indented, bool omitDeclaration, XmlSerializerNamespaces ns)
{
var utf8NoBom = new UTF8Encoding(false);
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = omitDeclaration,
Indent = indented,
Encoding = utf8NoBom
};
using (MemoryStream ms = new MemoryStream())
{
using …Run Code Online (Sandbox Code Playgroud) 我正在尝试将第一列的值读入数组.最好的方法是什么?下面是我到目前为止的代码.基本上我正在尝试获取该列的数据范围,以便将单元格值拉入系统数组.
Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.Interop.Excel.Application();
if (xlsApp == null)
{
Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
return null;
}
//xlsApp.Visible = true;
Workbook wb = xlsApp.Workbooks.Open(filename, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true);
Sheets sheets = wb.Worksheets;
Worksheet ws = (Worksheet)sheets.get_Item(1);
//***Breaks Here***
ListColumn column = ws.ListObjects[1].ListColumns[1];
Range range = column.DataBodyRange;
System.Array myvalues = (System.Array)range.Cells.Value;
Run Code Online (Sandbox Code Playgroud)