xml-serialization 相关问题

此标记指的是使用XML作为数据格式的序列化技术。

强制 XML 序列化以序列化只读属性

在 C# 中,我有一个类,它有一个应该通过 XML 序列化的派生属性。但是,XML 序列化(默认情况下)不会序列化 read=only 属性。我可以通过定义来解决这个问题...

回答 5 投票 0

C# 将对象序列化为包含对象列表的 XML

我需要序列化 XML 以附加 API PUT 请求。 我正在使用 System.Xml.Serialization。 最终结果需要类似于以下内容: 我需要序列化一个 XML 以附加到 API PUT 请求。 我正在使用 System.Xml.Serialization. 最终结果需要类似于以下内容: <?xml version="1.0" encoding="UTF-8"?> <prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> <product> <id> <![CDATA[2678]]> </id> <cache_default_attribute> <![CDATA[0]]> </cache_default_attribute> <id_shop_default> <![CDATA[1]]> </id_shop_default> <reference> <![CDATA[2678]]> </reference> <supplier_reference> <![CDATA[]]> </supplier_reference> <location> <![CDATA[]]> </location> <width> <![CDATA[0.000000]]> </width> <height> <![CDATA[0.000000]]> </height> <depth> <![CDATA[0.000000]]> </depth> <weight> <![CDATA[0.000000]]> </weight> <quantity_discount> <![CDATA[0]]> </quantity_discount> <ean13> <![CDATA[]]> </ean13> <isbn> <![CDATA[]]> </isbn> <upc> <![CDATA[0]]> </upc> <mpn> <![CDATA[000.018]]> </mpn> <cache_is_pack> <![CDATA[0]]> </cache_is_pack> <cache_has_attachments> <![CDATA[0]]> </cache_has_attachments> <is_virtual> <![CDATA[0]]> </is_virtual> <state> <![CDATA[1]]> </state> <additional_delivery_times> <![CDATA[1]]> </additional_delivery_times> <delivery_in_stock> <language id="1"> <![CDATA[]]> </language> <language id="2"> <![CDATA[]]> </language> </delivery_in_stock> <delivery_out_stock> <language id="1"> <![CDATA[]]> </language> <language id="2"> <![CDATA[]]> </language> </delivery_out_stock> <product_type> <![CDATA[combinations]]> </product_type> <on_sale> <![CDATA[0]]> </on_sale> <online_only> <![CDATA[0]]> </online_only> <date_add> <![CDATA[2023-02-16 00:21:12]]> </date_add> <date_upd> <![CDATA[2023-02-22 17:22:23]]> </date_upd> <name> <language id="1"> <![CDATA[ETNIES TRI LAM POLO (000.018)]]> </language> <language id="2"> <![CDATA[ETNIES TRI LAM POLO (000.018)]]> </language> </name> </product> </prestashop> 为此,我有一个这样的课程: [XmlType("product")] public class Product { public Product() { id = ""; active = "1"; available_for_order= "1"; indexed= "1"; visibility = "both"; delivery_in_stock = new List<PaLanguage>(); delivery_out_stock = new List<PaLanguage>(); name = new List<PaLanguage>(); } [XmlElement("id")] public XmlCDataSection C_id { get { return new XmlDocument().CreateCDataSection(id); } set { id = value.Value; } } [XmlIgnore] public string id; [XmlElement("cache_default_attribute")] public XmlCDataSection C_cache_default_attribute { get { return new XmlDocument().CreateCDataSection(cache_default_attribute); } set {cache_default_attribute = value.Value; } } [XmlIgnore] public string cache_default_attribute; ... [XmlArray("delivery_in_stock")] public List<PaLanguage> delivery_in_stock; [XmlArray("delivery_out_stock")] public List<PaLanguage> delivery_out_stock; ... PaLanguage 是: public class PaLanguage { [XmlElement("language")] public XmlCDataSection C_language { get { return new XmlDocument().CreateCDataSection(language); } set { language = value.Value; } } [XmlIgnore] public string language { get; set; } [XmlAttribute("id")] public string id; } 因为这是更新记录,所以我首先完成一个获取请求,然后匹配它的所有值。 我使用以下代码完成此操作: XDocument getResponse = Helper.GetProductReference(get, Helper.GetValueByKey("ShopApi") + "products/"); var gotProduct = getResponse .Element("prestashop") .Element("products") .Elements("product") .Where(e => e.Element("reference").Value == mtrl) .Single(); 我创建了一个新的产品项附加到我的放置请求,但我在序列化它的 XmlArrays 时遇到了问题。 我尝试的最后一件事是: foreach (XElement x in gotProduct.Element("delivery_in_stock").Elements("language")) { product.delivery_in_stock.Add(new PrestaLanguage { id = (string)x.Attribute("id"), language = (string)x.Element("language") }); } 这是完全错误的,因为它将它序列化为: <product> <id><![CDATA[2678]]></id> ... xml <delivery_in_stock> <PaLanguage id="1"> <language><![CDATA[]]></language> </PaLanguage> <PaLanguage id="2"> <language><![CDATA[]]></language> </PaLanguage> </delivery_in_stock> 感觉就像我已经尝试了所有可以在网上和官方文档中找到的东西,每次尝试都离我的预期结果更远。 我应该注意到这个类实际上还有几十个字段,它们要么只是一个字符串,要么是 id 和语言值的另一种组合。 感谢任何帮助。 似乎 XML 中的 delivery_in_stock 和 delivery_out_stock 元素可以包含具有不同 id 属性的多个语言元素。因此,您需要修改您的 foreach 循环以处理多种语言元素。 您可以使用 Descendants("language") 而不是使用 Elements("language") 来获取 delivery_in_stock 和 delivery_out_stock 下的所有语言元素。 foreach (XElement x in gotProduct.Element("delivery_in_stock").Descendants("language")) { product.delivery_in_stock.Add(new PaLanguage { id = (string)x.Attribute("id"), language = (string)x }); } foreach (XElement x in gotProduct.Element("delivery_out_stock").Descendants("language")) { product.delivery_out_stock.Add(new PaLanguage { id = (string)x.Attribute("id"), language = (string)x }); } 此外,在 PaLanguage 类中,语言属性应该是 XmlCDataSection 类型,而不是字符串,因为它包含 CDATA。 public class PaLanguage { [XmlText] public XmlCDataSection language { get; set; } [XmlAttribute("id")] public string id { get; set; } } 最后是product类,应该给没有的properties加上[XmlElement]属性,比如id_shop_default, reference, supplier_reference等 [XmlType("product")] public class Product { public Product() { id = ""; active = "1"; available_for_order = "1"; indexed = "1"; visibility = "both"; delivery_in_stock = new List<PaLanguage>(); delivery_out_stock = new List<PaLanguage>(); name = new List<PaLanguage>(); } [XmlElement("id")] public XmlCDataSection C_id { get { return new XmlDocument().CreateCDataSection(id); } set { id = value.Value; } } [XmlElement("cache_default_attribute")] public XmlCDataSection C_cache_default_attribute { get { return new XmlDocument().CreateCDataSection(cache_default_attribute); } set { cache_default_attribute = value.Value; } } [XmlElement("id_shop_default")] public XmlCDataSection C_id_shop_default { get { return new XmlDocument().CreateCDataSection(id_shop_default); } set { id_shop_default = value.Value; } } [XmlElement("reference")] public XmlCDataSection C_reference { get { return new XmlDocument().CreateCDataSection(reference); } set { reference = value.Value; } } [XmlElement("supplier_reference")] public XmlCDataSection C_supplier_reference { get { return new XmlDocument().CreateCDataSection(supplier_reference); } set { supplier_reference = value.Value; } } [XmlElement("location")] public XmlCDataSection C_location { get { return new XmlDocument().CreateCDataSection(location); } set { location = value.Value; } } [XmlElement("width")] public XmlCDataSection C_width { get { return new XmlDocument().CreateCDataSection(width); } set { width = value.Value; } } [XmlElement("height")] public XmlCDataSection C_height { get { return new XmlDocument().CreateCDataSection(height); } set { height = value.Value; } } // etc..

回答 1 投票 0

将 C# 类序列化为保持结构的 XML

我收到了 4 个 XSD 模式文件(一个是根,一个用于标题/尾部,一个用于基础,一个用于元素类型) 将根传递给 xsd.exe,成功生成具有适当

回答 0 投票 0

如何在 XStream 中拥有可选元素(XML -> Java 对象)

我正在将 XML 文档序列化为 Java 对象。有一些元素我想成为可选的。如果在 XML 中提供,那么它将最终添加额外的功能。但是,如果元素...

回答 0 投票 0

C# XML序列化如何设置属性 xsi:type

这是我的Xml在XML序列化后的样子。 . . . 这是我的C#代码: public class Valué { [XmlAttribute(...)

回答 1 投票 0

如何使用JAXB对非基元数据进行序列化?

我想使用JAXB对一个对象进行序列化。我的类定义是这样的。@XmlRootElement public class MyClass{ < > private xyz_type xyz; @...

回答 1 投票 0

忽略列表中的属性的XmlIgnore。

我如何忽略我试图序列化的另一个类型的列表中的属性? public string SerializeTag(List) reservedTags) { string ...

回答 1 投票 1

XML序列化,为什么我最后会有一个结束的元素?

我在这里做错了什么?测试XML。 ...

回答 1 投票 0

如何在XML序列化中省略C#对象属性名节点?

我的模型类是: public class Group { public Employee Employee {get;set;}。} public class Employee { public string Name {get;set;}。}所以在序列化之后,我的xml看起来是这样的。

回答 1 投票 0

我需要序列化一个有字符串字段的对象,字符串的值是 "Apply",当对象被序列化时,我得到的是 "Apply"。

回答 1 投票 0

对C#类的Soap响应总是返回空值。

我很难从web服务解析soap响应并将其转换为c#对象。我所做的一切,它总是返回空元素。这里是我的响应。

回答 1 投票 0

XmlSerializer访问父元素中的子逻辑元素

我正在使用标准的.NET XmlSerializer对以下xml进行反序列化: [[[First

回答 2 投票 1

如何使用C ++对自定义的结构变量进行xml序列化?

[如果有这样的结构:structrect {double length;双倍宽度};结构a; a.length = 1; a.width = 2;我想将其序列化为这样的xml文件: ...

回答 1 投票 0

“ xsi:type”属性更改为“ type” c#xml文档

我正在使用XMLDocument创建XML文件,但是当我在.xml生成的文件中将属性设置为名为“ xsi:type”的元素时,该属性将更改为“ type”。这是i'...

回答 1 投票 0

“ XML文件中的“数据库”结构

因此,我必须在xml文档中定义一个“数据库”,该文档必须具有学校,年级,房间,课程和学生,然后我必须用C#编写一些类,该类必须获取并保存数据到...

回答 1 投票 0

如何将XML数组/列表序列化为顺序命名的元素

我需要在C#/。net中序列化XML的帮助。在C#中给出了类似的内容:public class Action {public string Name;公共字符串类型; }公共类TestObject {公共列表

回答 2 投票 0

为什么在XML序列化期间将名称空间添加到子节点?

[我有一个用于XML序列化的类,如下所示:[XmlRoot(ElementName =“ XXX”,Namespace =“ http://www.example.com/Schemas/xxx/2011/11”)]公共类Xxx 其中T:装运{[...] >>

回答 1 投票 1

XML停止覆盖/保存多个条目

目前,我正在使用此代码创建XML文件{XmlSerializer sr = new XmlSerializer(obj.GetType()); TextWriter writer =新的StreamWriter(filename); sr ....

回答 1 投票 0

[如果我们有使用JAXB时未初始化的最终字段,如何引入无参数构造函数?

我正在尝试使用JAXB将我的Java对象序列化为XML。为了使用JAXB,必须序列化的类必须具有no参数构造函数。但是我的问题是所有的领域都是最终的...

回答 1 投票 0

具有xml-element的xml-元素的XML序列化

我必须创建一个XML文档,以便在其他软件中进行导入。导入过程无法修改,因此xml及其结构是固定的。我在visualstudio中使用VB.net。我使用“ Xml ....

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.