我正在尝试形成一个 XML 文档,我将使用它通过 HTTPS 将其发送到 API,但是我注意到,即使我已将 XDeclaration 元素添加到我的 XML,XDeclaration 也没有出现在字符串中我返回使用 xmlDoc.ToString()方法。
有谁知道我是否缺少特定设置或 <?xml version="1.0" encoding="UTF-8" ?> 的任何原因?元素没有出现?
xmlDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("NABTransactMessage",
new XElement("MessageInfo",
new XElement("MessageID", "5167813675aa47d181a7c76979f2de00"),
new XElement("MessageTimeStamp", "20152701024752898882+000"),
new XElement("timeoutValue", 60),
new XElement("apiVersion", "spxml-4.2")
),
new XElement("MerchantInfo",
new XElement("MerchantID", "XYZ0010"),
new XElement("password", "abcd1234")
),
new XElement("RequestType", "Periodic"),
new XElement("Periodic",
new XElement("PeriodicList", new XAttribute("count", 1),
new XElement("PeriodicItem", new XAttribute("ID", 1),
new XElement("actionType", "addcrn"),
new XElement("periodicType", 5),
new XElement("crn", "85c2960d-1422326872"),
new XElement("CreditCardInfo",
new XElement("cardNumber", 4111111111111111),
new XElement("expiryDate", "08/20"),
new XElement("cvv", 123)
)
)
)
)
)
);
return xmlDoc.ToString(SaveOptions.None);
通过 HTTPS 发送请求的代码:
public static string SendRequest(string requestContent, string requestContentType, string requestUrl)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
byte[] bytes;
bytes = System.Text.Encoding.UTF8.GetBytes(requestContent);
request.ContentType = requestContentType + "; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
//request.Timeout = 5000;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(requestContent, 0, requestContent.Length);
}
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
return new StreamReader(responseStream).ReadToEnd();
}
}
}
备注:xmlDoc.ToString()值正在传递给 SendRequest()作为第一个参数,requestContentType被设置为 "text/xml"
最佳答案
XDocument.ToString() 不包含声明。相反,使用 XDocument.Save() ,例如:
public static string ToXml(this XDocument xDoc)
{
StringBuilder builder = new StringBuilder();
using (TextWriter writer = new StringWriter(builder))
{
xDoc.Save(writer);
return builder.ToString();
}
}
如果您特别需要将编码字符串设为“UTF-8”,请参见此处:Force XDocument to write to String with UTF-8 encoding
请注意,此扩展适用于 XDocument,而不是具有 OuterXml 的 XmlDocument。
关于c# - XDocument XDeclaration 未出现在 ToString 结果中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28183461/
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
导读语言模型给我们的生产生活带来了极大便利,但同时不少人也利用他们从事作弊工作。如何规避这些难辨真伪的文字所产生的负面影响也成为一大难题。在3月9日智源Live第33期活动「DetectGPT:判断文本是否为机器生成的工具」中,主讲人Eric为我们讲解了DetectGPT工作背后的思路——一种基于概率曲率检测的用于检测模型生成文本的工具,它可以帮助我们更好地分辨文章的来源和可信度,对保护信息真实、防止欺诈等方面具有重要意义。本次报告主要围绕其功能,实现和效果等展开。(文末点击“阅读原文”,查看活动回放。)Ericmitchell斯坦福大学计算机系四年级博士生,由ChelseaFinn和Chri
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
我正在尝试在配备ARMv7处理器的SynologyDS215j上安装ruby2.2.4或2.3.0。我用了optware-ng安装gcc、make、openssl、openssl-dev和zlib。我根据README中的说明安装了rbenv(版本1.0.0-19-g29b4da7)和ruby-build插件。.这些是随optware-ng安装的软件包及其版本binutils-2.25.1-1gcc-5.3.0-6gconv-modules-2.21-3glibc-opt-2.21-4libc-dev-2.21-1libgmp-6.0.0a-1libmpc-1.0.2-1libm
下面的代码工作正常:person={:a=>:A,:b=>:B,:c=>:C}berson={:a=>:A1,:b=>:B1,:c=>:C1}kerson=person.merge(berson)do|key,oldv,newv|ifkey==:aoldvelsifkey==:bnewvelsekeyendendputskerson.inspect但是如果我在“ifblock”中添加return,我会得到一个错误:person={:a=>:A,:b=>:B,:c=>:C}berson={:a=>:A1,:b=>:B1,:c=>:C1}kerson=person.merge(berson
我正在使用macos,我想使用ruby驱动程序连接到sqlserver。我想使用tiny_tds,但它给出了缺少free_tds的错误,但它已经安装了。怎么能过这个?~brewinstallfreetdsWarning:freetds-0.91.112alreadyinstalled~sudogeminstalltiny_tdsBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtiny_tds:ERROR:Failedtobuildgemnativeextension.完整日志如下:/System
我有以下haml:9%strongAskedby:10=link_to@user.full_name,user_path(@user)11.small="(#{@question.created_at.strftime("%B%d,%Y")})"这当前将链接和日期放在不同的行上,当它看起来像“链接(日期)”并且日期的类跨度为小...... 最佳答案 您的代码将生成类似这样的html:Askedby:UsernameApril26,2011当您使用类似.small的东西(即使用点而不指定元素类型)时,haml会创建一个implicit
我如何做Ruby方法"Flatten"RubyMethod在C#中。此方法将锯齿状数组展平为一维数组。例如:s=[1,2,3]#=>[1,2,3]t=[4,5,6,[7,8]]#=>[4,5,6,[7,8]]a=[s,t,9,10]#=>[[1,2,3],[4,5,6,[7,8]],9,10]a.flatten#=>[1,2,3,4,5,6,7,8,9,10 最佳答案 递归解决方案:IEnumerableFlatten(IEnumerablearray){foreach(variteminarray){if(itemisIEnume
我最近从C#转向了Ruby,我发现自己无法制作可折叠的标记代码区域。我只是想到做这种事情应该没问题:classExamplebegin#agroupofmethodsdefmethod1..enddefmethod2..endenddefmethod3..endend...但是这样做真的可以吗?method1和method2最终与method3是同一种东西吗?还是有一些我还没有见过的用于执行此操作的Ruby惯用语? 最佳答案 正如其他人所说,这不会改变方法定义。但是,如果要标记方法组,为什么不使用Ruby语义来标记它们呢?您可以使用