此标记指的是使用XML作为数据格式的序列化技术。
C#.NET XML 序列化:XMLAttribute 未设置名称空间,只有 UserName 对象中的密码元素具有属性
我正在尝试生成此 Soap XML: 我正在尝试生成此 Soap XML: <soapenv:Envelope xmlns:bsvc="urn:com.workday/bsvc" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header> <wsse:Security soapenv:mustUnderstand="1"> <wsse:UsernameToken> <wsse:Username>TestUser02</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password1</wsse:Password> </wsse:UsernameToken> </wsse:Security> <bsvc:Workday_Common_Header> <bsvc:Include_Reference_Descriptors_In_Response>1</bsvc:Include_Reference_Descriptors_In_Response> </bsvc:Workday_Common_Header> </soapenv:Header> <soapenv:Body> <bsvc:Get_Customers_Request bsvc:version="v42.0"> <bsvc:Request_References> <bsvc:Customer_Reference bsvc:Descriptor="?"> <bsvc:ID bsvc:type="Customer_ID">100001</bsvc:ID> </bsvc:Customer_Reference> </bsvc:Request_References> <bsvc:Response_Group> <bsvc:Include_Reference>0</bsvc:Include_Reference> <bsvc:Include_Customer_Data>0</bsvc:Include_Customer_Data> <bsvc:Include_Customer_Balance>1</bsvc:Include_Customer_Balance> <bsvc:Include_Customer_Activity_Detail>0</bsvc:Include_Customer_Activity_Detail> </bsvc:Response_Group> </bsvc:Get_Customers_Request> </soapenv:Body> </soapenv:Envelope> 但我使用下面的代码获取此 XML: <soapenv:Envelope xmlns:bsvc="urn:com.workday/bsvc" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header> <wsse:Security soapenv:mustUnderstand="1"> <wsse:UsernameToken Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"> <wsse:Username>TestUser02</wsse:Username> <wsse:Password>Password1</wsse:Password> </wsse:UsernameToken> </wsse:Security> <bsvc:Workday_Common_Header> <bsvc:Include_Reference_Descriptors_In_Response>1</bsvc:Include_Reference_Descriptors_In_Response> </bsvc:Workday_Common_Header> </soapenv:Header> <soapenv:Body> <bsvc:Get_Customers_Request version="v42.0"> <bsvc:Request_References> <bsvc:Customer_Reference Descriptor="?"> <bsvc:ID type="Customer_ID">100001</bsvc:ID> </bsvc:Customer_Reference> </bsvc:Request_References> <bsvc:Response_Group> <bsvc:Include_Reference>0</bsvc:Include_Reference> <bsvc:Include_Customer_Data>0</bsvc:Include_Customer_Data> <bsvc:Include_Customer_Balance>1</bsvc:Include_Customer_Balance> <bsvc:Include_Customer_Activity_Detail>0</bsvc:Include_Customer_Activity_Detail> </bsvc:Response_Group> </bsvc:Get_Customers_Request> </soapenv:Body> </soapenv:Envelope> 这是我的 C# 代码: using System; using System.Collections.Generic; using System.Xml.Serialization; namespace WorkDayAPI { [XmlRoot("Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] public class Envelope { [XmlElement("Header")] public Header Header { get; set; } [XmlElement("Body")] public Body Body { get; set; } } public class Header { [XmlElement("Security", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] public Security Security { get; set; } [XmlElement("Workday_Common_Header", Namespace = "urn:com.workday/bsvc")] public WorkdayCommonHeader WorkdayCommonHeader { get; set; } } public class Security { [XmlAttribute("mustUnderstand", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] public string MustUnderstand { get; set; } [XmlElement("UsernameToken", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] public UsernameToken UsernameToken { get; set; } } public class UsernameToken { [XmlElement("Username")] public string Username { get; set; } [XmlElement("Password", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] public string Password { get; set; } [XmlAttribute("Type")] public string PasswordType { get; set; } } public class WorkdayCommonHeader { [XmlElement("Include_Reference_Descriptors_In_Response", Namespace = "urn:com.workday/bsvc")] public string IncludeReferenceDescriptorsInResponse { get; set; } } public class Body { [XmlElement("Get_Customers_Request", Namespace = "urn:com.workday/bsvc")] public GetCustomersRequest GetCustomersRequest { get; set; } } public class GetCustomersRequest { [XmlElement("Request_References", Namespace = "urn:com.workday/bsvc")] public RequestReferences RequestReferences { get; set; } [XmlAttribute("version", Namespace = "urn:com.workday/bsvc")] public string Version { get; set; } [XmlElement("Response_Group", Namespace = "urn:com.workday/bsvc")] public ResponseGroup ResponseGroup { get; set; } } public class RequestReferences { [XmlElement("Customer_Reference", Namespace = "urn:com.workday/bsvc")] public CustomerReference CustomerReference { get; set; } } public class CustomerReference { [XmlAttribute(AttributeName = "Descriptor", Namespace = "urn:com.workday/bsvc")] public string Descriptor { get; set; } [XmlElement(ElementName = "ID")] public CustomerData CustomerData { get; set; } } [XmlType(Namespace = "urn:com.workday/bsvc")] public class CustomerData { [XmlText] public string ID { get; set; } [XmlAttribute(AttributeName = "type", Namespace = "urn:com.workday/bsvc")] public string Type { get; set; } } public class ResponseGroup { [XmlElement("Include_Reference", Namespace = "urn:com.workday/bsvc")] public string IncludeReference { get; set; } [XmlElement("Include_Customer_Data", Namespace = "urn:com.workday/bsvc")] public string IncludeCustomerData { get; set; } [XmlElement("Include_Customer_Balance", Namespace = "urn:com.workday/bsvc")] public string IncludeCustomerBalance { get; set; } [XmlElement("Include_Customer_Activity_Detail", Namespace = "urn:com.workday/bsvc")] public string IncludeCustomerActivityDetail { get; set; } } } 这就是我设置命名空间和分配值的方式: using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using WorkDayAPI.Input; namespace WorkDayAPI { class Program { static void Main(string[] args) { GenerateSoapXmlRequest(); } public static void GenerateSoapXmlRequest() { // Create the request object and populate it with values var soapRequest = new Envelope { Header = new Header { Security = new Security { MustUnderstand = "1", UsernameToken = new UsernameToken { Username = "TestUser02", Password = "Password1", PasswordType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText" } }, WorkdayCommonHeader = new WorkdayCommonHeader { IncludeReferenceDescriptorsInResponse = "1" } }, Body = new Body { GetCustomersRequest = new GetCustomersRequest { Version = "v42.0", RequestReferences = new RequestReferences { CustomerReference = new CustomerReference { Descriptor = "?", CustomerData = new CustomerData { Type = "Customer_ID", ID = "100001" } } }, ResponseGroup = new ResponseGroup { IncludeReference = "0", IncludeCustomerData = "0", IncludeCustomerBalance = "1", IncludeCustomerActivityDetail = "0" } } } }; // Prepare the namespaces with appropriate prefixes var namespaces = new XmlSerializerNamespaces(); namespaces.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"); namespaces.Add("bsvc", "urn:com.workday/bsvc"); namespaces.Add("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); // Create an XmlSerializer instance for the Envelope class var serializer = new XmlSerializer(typeof(Envelope)); // Create a StringWriter to hold the serialized XML using (var stringWriter = new StringWriter()) { // Create an XmlWriterSettings object to control XML formatting var xmlSettings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true, OmitXmlDeclaration = true }; // Create an XmlWriter with the settings and namespaces using (var xmlWriter = XmlWriter.Create(stringWriter, xmlSettings)) { // Serialize the object to XML, passing the namespaces serializer.Serialize(xmlWriter, soapRequest, namespaces); } // Get the generated XML as a string string soapXml = stringWriter.ToString(); // Print the serialized XML to the console Console.WriteLine(soapXml); // Optionally, save the XML to a file File.WriteAllText("SoapRequest.xml", soapXml); } } } } 如何修复密码中的 Type 属性? 如何在Body属性中设置bsvc的Namespace? 以下是所需 XML 与生成 XML 之间的比较差异: ComparisonPic:[![enter image description here][1]][1] 第一个问题通过在密码类中将密码设置为XMLText并将PasswordType设置为XMLAttribute来解决,然后正确生成XML。仍然没有获得 BSVC 内部属性的命名空间: public class UsernameToken { [XmlElement("Username")] public string Username { get; set; } [XmlElement("Password")] public PasswordData Password { get; set; } } public class PasswordData { [XmlText] public string Password { get; set; } [XmlAttribute("Type")] public string PasswordType { get; set; } }
为什么反序列化子类时会引发 XmlSerializer.UnknownAttribute/UnknownNode 事件?
当使用子类时,我在反序列化由 XmlSerializer 生成的 XML 代码时遇到了问题。 UnknownAttribute 和 UnknownNode 事件触发,我认为存在错误
我有一些派生类要序列化为 XML: 公共抽象类级别 { } [XmlRoot("SubLevel1")] 公开课 SubLevel1 : 级别 { 公共字符串 prop1 { 得到;放; } p...
为自定义 XmlSerializer 生成 Xml 序列化程序集
我的类中有使用与默认序列化器生成的 XML 结构不同的 XML 结构进行序列化/反序列化的方法。这些方法为我的类型创建一个 XmlSerializer,但是...
我正在尝试用一个类读取两个 Aux 文件,但遇到了麻烦。 当我尝试序列化我的类时出现错误。 xml 架构是由第三方定义的,我无法更改它。 该...
如何防止 XML::LibXML 制作 <style> CDATA?
假设我不希望这种情况发生在我的 标签上: ./ph nongdi.html | grep 样式 nongdi.html - 农地.html: <style> 农地.html: (标准输入):...</desc> <question vote="-2"> <p>我不希望这种情况发生在我的 <pre><code><style></code></pre> 标签上:</p> <pre><code>./ph nongdi.html | grep style nongdi.html - nongdi.html: <style> nongdi.html: </style> (standard input): <style><![CDATA[ (standard input): ]]></style> </code></pre> <p>是的,我读过<a href="https://stackoverflow.com/questions/3302648/should-i-use-cdata-in-html5">我应该在 HTML5 中使用 <![CDATA[...]]>吗?</a>但这不是重点。</p> <p>我做错了什么?</p> <pre><code>$ cat ph #!/usr/bin/perl use XML::LibXML; use strict; use warnings q(all); my $parser = XML::LibXML->new( no_cdata => 1 ); my $dom = $parser->load_html( location => shift ); print $dom->toString(); </code></pre> </question> <answer tick="false" vote="0"> <p>您无法更改它,因为这就是 parse_html 的工作原理。所以改用:</p> <pre><code>my $n = $dom->toString(); for ($n) { s/\Q<![CDATA[\E//; s/]]>//; } print $n; </code></pre> </answer> </body></html>
有没有一种好方法来处理在 C# 中可以具有带有变量名称的嵌套节点的文件的 XML 反序列化?
我正在用 C# 开发一个用于 XML 文件类型的反序列化器,该程序的程序我无法控制。不幸的是,XML 文件结构在两个主要方面完全打破了约定,到目前为止......
我尝试使用 XMLMapper 将一些配置类序列化为 xml 配置文件。 但我在属性生成方面遇到了一些麻烦。实际上生成的 XML 是完美的,但是 XMLMapper 添加了一些......
我想将对象序列化为soap请求。 我创建一个登录请求。 我使用 xml 序列化。 我不知道我应该做什么来改变这条线: 我想将对象序列化为soap请求。 我创建一个登录请求。 我使用 xml 序列化。 我不知道我应该做什么来改变这条线: <login xmlns="http://ws.com/"> 对此: <tas:login> 我这样做了: using System; using System.Text; using System.Xml; using System.Xml.Serialization; public class Program { public static void Main() { var env = new LoginRequest.Envelope { Header = new LoginRequest.Header(), Body = new LoginRequest.Body() { LoginRequest = new LoginRequest { // Id = "o0", // Root = 1, DeviceId = "***", Firm = "***", Login = "***", Password = "***" }, }, }; var serializer = new XmlSerializer(typeof(LoginRequest.Envelope)); var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true, OmitXmlDeclaration = true, }; var builder = new StringBuilder(); using (var writer = XmlWriter.Create(builder, settings)) { serializer.Serialize(writer, env, env.xmlns); } Console.WriteLine(builder.ToString()); } } [XmlType(Namespace = LoginRequest.t, IncludeInSchema = true)] public class LoginRequest { private const string i = "http://www.w3.org/2001/XMLSchema-instance"; private const string d = "http://www.w3.org/2001/XMLSchema"; private const string c = "http://schemas.xmlsoap.org/soap/encoding/"; private const string v = "http://schemas.xmlsoap.org/soap/envelope/"; private const string t = "http://ws.com/"; // [XmlAttribute(AttributeName = "id")] // public string Id { get; set; } // [XmlAttribute(AttributeName = "root", Namespace = c)] // public int Root { get; set; } [XmlElement(ElementName = "firm")] public string Firm { get; set; } [XmlElement(ElementName = "login")] public string Login { get; set; } [XmlElement(ElementName = "password")] public string Password { get; set; } [XmlElement(ElementName = "device_id")] public string DeviceId { get; set; } [XmlRoot(Namespace = v)] public class Envelope { public Header Header { get; set; } public Body Body { get; set; } static Envelope() { staticxmlns = new XmlSerializerNamespaces(); staticxmlns.Add("i", i); staticxmlns.Add("d", d); staticxmlns.Add("c", c); staticxmlns.Add("soapenv", v); } private static XmlSerializerNamespaces staticxmlns; [XmlNamespaceDeclarations] public XmlSerializerNamespaces xmlns { get { return staticxmlns; } set { } } } [XmlType(Namespace = v)] public class Header { } [XmlType(Namespace = v)] public class Body { [XmlElement(ElementName = "login", Namespace = t)] public LoginRequest LoginRequest { get; set; } } } 这是我的输出: <soapenv:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header /> <soapenv:Body> <login xmlns="http://ws.com/"> <firm>***</firm> <login>***</login> <password>***</password> <device_id>***</device_id> </login> </soapenv:Body> </soapenv:Envelope> 我想要这个: <Envelope xmlns:soapenv="http://ws.com/" xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <Header /> <Body> <tas:login> <soapenv:firm>***</soapenv:firm> <soapenv:login>***</soapenv:login> <soapenv:password>***</soapenv:password> <soapenv:device_id>***</soapenv:device_id> </tas:login> </Body> </Envelope> 我不知道我应该做什么来改变这条线: <login xmlns="http://ws.com/"> 对此: <tas:login> 你现在可能已经开始工作了,但以防万一。我相信你所要做的一切都会改变 [XmlElement(ElementName = "firm")] public string Firm { get; set; } 到 [XmlElement(ElementName = "firm", Namespace = v)] public string Firm { get; set; } 这应该改变 <firm>***</firm> 到 <soapenv:firm>***</soapenv:firm>
我使用以下代码反序列化 xml 配置文件: // 通过反序列化给定的 xml 文档创建对象 var 序列化器 = new XmlSerializer(typeof(ConfigurationFile)); var st...
C# - 如何将内部类序列化(反)序列化为带有属性的 XML?
我想(反)序列化内部类。 内部类数据 { 公共字符串?名称{获取;放; } 公共字符串?类型{获取;放; } 公共列表 DataList { 获取; ...
C# - 我如何将内部类序列化(反)序列化为带有属性的 XML?
我想(反)序列化内部类。 内部类数据 { 公共字符串?名称{获取;放; } 公共字符串?类型{获取;放; } 公共列表 DataList { 获取; ...
我正在为 XML 数据开发一个轻量级类包装器,遵循 http://packages.python.org/dexml/api/dexml.fields.html 或 https://gist.github.com/ 485977。这些类包含一个元素T...
我的目标是输出如下例所示的 XML: 我的目标是输出如下例所示的 XML: <FunctionList name="list1"> <Space title="RoomA"> <Space title="Cabinet1"> <Device title="Device1"/> <Device title="Device2"/> </Space> <Space title="Cabinet2"> <Device title="Device3"/> </Space> </Space> </Locations> 然而,序列化器输出包含子元素的变量。 <FunctionList name="list1"> **<elements>** <Space title="RoomA"> **<Children>** <Space title="Cabinet1"> **<Children>** <Device title="Device1"/> <Device title="Device2"/> **</Children>** </Space> <Space title="Cabinet2"> **<Children>** <Device title="Device3"/> **</Children>** </Space> **</Children>** </Space> **</elements>** </Locations> 我想去掉粗体的 xml 元素,因为它们公开了 C# 变量,从 XML 结构中可以明显看出它是父子关系。然而,从我的角度来看,这可能是错误的逻辑。 XML 标签和类 Space 和 Device 是 DesignElement 的子类。 [Serializable] public class FunctionList { [XmlAttribute("name")] public string Name { get; set; } [XmlArray("Element")] [XmlArrayItem(nameof(Space), typeof(Space))] [XmlArrayItem(nameof(Device), typeof(Device))] public List<DesignElement> elements { get; set; } } [Serializable] public abstract class DesignElement { [XmlAttribute("title")] public string Title { get; set; } } [Serializable] public class Space : DesignElement { [XmlArray("Children")] [XmlArrayItem(nameof(Space), typeof(Space))] [XmlArrayItem(nameof(Device), typeof(Device))] public List<DesignElement> children { get; set; } } [Serializable] public class Device: DesignElement { } 我用 [XmlArray("Children")] 和 [XmlArray("Element")] 尝试了多种方法。我认为解决方案有点像 [XmlElement(typeof(object))] 但我找不到正确的语法。但也许我对这个问题的角度是错误的。 未经测试,但我相信你想要: [XmlElement(nameof(Space), typeof(Space))] [XmlElement(nameof(Device), typeof(Device))] public List<DesignElement> children { get; set; } // or elements
如何使用 C++ boost::serialization 处理可选 Xml 元素
我正在使用 C++ boost::序列化库来读取和写入配置 XML。为了向用户提供向后兼容性,在读取 XML 时,如果缺少某些 XML 元素,则需要具有
我有一个属性定义为: [XmlArray("删除", IsNullable = true)] [XmlArrayItem("联系人", typeof(ContactEvent)), XmlArrayItem("sms", typeof(SmsEvent))] 公共列表 删除 { ge...
我有几个类,一旦设置了初始值,它们就是不可变的。 Eric Lippert 将这种现象称为一次写入不变性。 在 C# 中实现一次写入不变性通常意味着设置 i...
这是我的课: [XmlRoot(ElementName = "包")] 公共类包:MyBase { 公众留言 留言 { 获取{返回_消息; } 设置 {
我有一个继承的C#类。我已经成功地“构建”了该对象。但我需要将对象序列化为 XML。有简单的方法吗? 看来这个班已经开课了
我正在序列化一个 XML 文件,其中包含使用 XML 的特殊粘贴自动创建的类。我做的一切都没有问题,超过3000行(Xml文件超过600行),但是我...