LINQ-to-XML提供类似SQL的查询体验,用于导航,解析和聚合存储在XML文档中的数据。
C# 使用 Linq 从 Datatable 创建 XML 文件
我是 XML 的新手,想从数据表创建 XML 文档 我有一个包含以下数据的数据表: https://i.stack.imgur.com/rHTE2.png 在此处输入图像描述 在此处输入图像描述 ...
我需要序列化 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..
System.Xml.Linq.XContainer.Element(...) 返回 null
我有 postXmlData 方法来发布请求并在 Xml 中获得响应。收到回复后,我试图显示属性名称以(“f”)开头,但出现错误 System.Xml.Linq.XContai ...
XDocument.parse将字符串作为一个元素进行解析。
当我使用XDocument.parse时,它将整个字符串解析为一个元素,解析后的文档没有任何结构,我正在使用regex来查找我需要工作的XML字符串,这是我的代码int total=1; ....
在ASP.NET MVC中,我有这样的代码:我想把datareader中的数据保存在xml文件中,我使用了下面的代码,但我在文件中只有两行。 &...
我试图循环浏览一个LINQXML查询的结果。当我使用.Element(1).Value时,我可以返回一个字符串结果,但现在我想返回所有的Elements()并循环浏览它们。我显示...
使用LINQ方法语法将XML数据放入Dictionary对象中
我想将XML文档解析为Dictionary对象。结构如下.. [[[[[12345 科罗拉多&...
我有基于Microsoft文档的代码。 https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/linq/how-to-write-queries-with-complex-filtering字符串xml =“ ” ...] >
为什么FirstNode.GetType()返回XElement而不是XNode
c#文档说FirstNode属性返回一个XNode。公共System.Xml.Linq.XNode FirstNode {get; }但是,当我在其上调用GetType()方法时,它表示System.Xml.Linq.XElement为...
使用Linq到XML查询google sitemap.xml的问题
我有一个Linq-2-XML查询,如果我创建的Google站点地图的urlset元素中填充了属性,则该查询将不起作用,但如果不存在属性,该查询将正常运行。无法...
我正在尝试从XML文档中获取元素列表,其中节点具有特定的属性值。该文档的结构如下: ...
考虑XML: Cake 0.55 ]] 您应该能够使用Parent属性,计算步骤,直到到达根目录为止: public int GetElmtDepth(XDocument doc, XElement elmt) { var (depth, target) = (0, elmt); while (target != doc.Root) (depth, target) = (depth + 1, target.Parent); return depth; } 您可以从System.Xml.XmlTextReader.Depth简化MSDN示例以显示节点元素及其各自的深度: // XML file to be parsed. string xmlFilePath = @"C:\test.xml"; // Create the reader. using XmlTextReader reader = new XmlTextReader(xmlFilePath); // Parse the XML and display each node. while (reader.Read()) { // If node type is an element // Display element name and depth if (reader.NodeType == XmlNodeType.Element) { Console.WriteLine($"Element = {reader.Name}, Depth = {reader.Depth}"); } } 输出: Element = items, Depth = 0 Element = item, Depth = 1 Element = name, Depth = 2 Element = ppu, Depth = 2 Element = batters, Depth = 2 Element = batter, Depth = 3 Element = batter, Depth = 3 Element = batter, Depth = 3 Element = topping, Depth = 2 Element = topping, Depth = 2 Element = topping, Depth = 2 Element = topping, Depth = 2 Element = topping, Depth = 2 Element = topping, Depth = 2 未来的读者可能希望看到用于计算元素深度的纯XPath解决方案: [使用任何方法选择目标元素,例如,id://*[@id="1001"]。 选择所有祖先://*[@id="1001"]/ancestor::* 计算那些祖先:count(//*[@id="1001"]/ancestor::*)。 按预期,这将为给定的XML返回3。
如何将XML子节点添加到父节点c#,我当前的xml文件如下所示: [45 ...
[[[[[[[[[[[[[>] 您应该考虑按顺序查询XML report =>location => day ==> hour。尝试一下:
我知道XML文件需要一个根元素才能正常运行。我正在尝试创建XML文件的一部分,然后将其附加到另一个格式正确的XML ...