linq-to-xml 相关问题

LINQ-to-XML提供类似SQL的查询体验,用于导航,解析和聚合存储在XML文档中的数据。

使用 LINQ 和 XDocument 获取 XML 的子级

我可以解析 XML 并从中获取这个特定的块,如下所示: var document = XDocument.Parse(xml); var 信封状态元素 = document.Root .Elements() .

回答 1 投票 0

如何在C#中使用XDocument从Xml文件中读取<?Insert-Table ID="hebz-9780192893512-chapter-1-tableGroup-1"?>标签

在 xml 文件中,标签以 开头 在 xml 文件中,标签以 开头 如何读取xml标签 我使用 System.Xml.Linq.XDocument 来读取 xml 文件。我无法读取标签如何读取此标签。我用过XDocument 这些“标签”称为处理指令,在 Linq 中它们使用 XProcessingInstruction 类表示。

回答 1 投票 0

如何将 xml 文件加载到 XStreamingElement 中?

有人有如何从 XML 文件填充 XStreamingElement 的示例吗?谢谢。

回答 1 投票 0

使用 LINQ to XML 获取元素值列表不起作用

我有一个 XML 文件,其中包含以下数据: 我有一个 XML 文件,其中包含以下数据: <?xml version="1.0" encoding="utf-8"?> <PublisherDatabase xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="4" xmlns="http://www.publictalksoftware.co.uk/msa"> <Publishers> <Publisher> <Name>Mr Happy</Name> </Publisher> <Publisher> <Name>Mr Sad</Name> </Publisher> </Publishers> </PublisherDatabase> 这是一个片段,精简到了骨架。 现在,我尝试使用 LINQ to XML 来获取所有 List<string> 值的 Name。 这将返回 0 个项目的列表: XDocument xmlDoc = XDocument.Load(publisherDataPath); var list = xmlDoc.Root.Elements("Publishers").Elements("Name") .Select(element => element.Value) .ToList(); Console.WriteLine(list.ToString()); 我的错误是什么?然后我尝试了: var list = xmlDoc.Root.Elements("Publishers").Elements("Publisher").Elements("Name") .Select(element => element.Value) .ToList(); Console.WriteLine(list.ToString()); 最终还是0。 这些 xml 元素属于 http://www.publictalksoftware.co.uk/msa xml 命名空间。 您必须在 LINQ 查询中为每个元素指定该元素。 var ns = XNamespace.Get("http://www.publictalksoftware.co.uk/msa"); var list = xmlDoc.Root.Elements(ns + "Publishers").Elements(ns + "Publisher").Elements(ns + "Name") .Select(element => element.Value) .ToList();

回答 1 投票 0

linq 访问深层 xml 数据时出现问题

我正在尝试使用 linq 访问 xml 文件深处的 xml 数据。我一直在看这些例子: 林克 林克 我没有看到一个不涉及名字的好例子。 到目前为止我有这个代码:

回答 1 投票 0

在 c# 中使用 Linq 将一组 xml 值传递给对象

创建了一个类来帮助从 xml 传递值: 类 ApptDetails { // 公共字符串日期 { 获取;放; } 公共字符串开始时间{获取;放; } 公共字符串结束时间{获取;放; } 噗...

回答 1 投票 0

XDocument 无法在 C# LINQ 中加载版本 1.1 的 xml?

XDocument.Load 在使用版本 1.1 而不是 1.0 的 XML 文件时引发异常: 未处理的异常:System.Xml.XmlException:版本号“1.1”无效。 1 号线,16 号位置。 任何...

回答 3 投票 0

将部分 XML 字符串解析为 XElements 列表

我有一个没有根元素的部分 XML 字符串。例如。: 一些文字 其他一些文字 我需要转换这个字符串...

回答 2 投票 0


如何从 XElement 中删除特定节点?

我创建了一个带有节点的 XElement,其 XML 如下所示。 我想删除所有包含“条件”节点的“规则”节点。 我创建了一个 for 循环,如下所示,但它没有

回答 6 投票 0

如何从 XElement 中删除特定节点?

我创建了一个带有节点的 XElement,其 XML 如下所示。 我想删除所有包含“条件”节点的“规则”节点。 我创建了一个 for 循环,如下所示,但它不会删除我的节点

回答 6 投票 0

.net 当名称带有冒号时读取 xml 元素值

我正在使用 VB.Net,想要从 xml 文件(它是来自 DarkTable 的 xmp 文件)读取特定值。我想阅读下面指示为“***此值...

回答 1 投票 0

从 XAttribute 中删除命名空间

如果能够从classificationType 属性中删除命名空间(xmlns:icls="http://www.aade.gr/myDATA/invoice/v1.0"),那就太好了。 到目前为止的代码: 公共 XElement BuildX...

回答 1 投票 0

XElement 显式运算符在 XElement 为 null 时抛出异常

XElement 对于可空值具有显式运算符。例如可为空。 Microsoft 提供的示例似乎可以在 WriteLine 中容纳 null 值,但如果您提供 null 它...

回答 1 投票 0

使用 C# 从 xml 内部元素获取特定值

我有一个 XML 文件,其数据如下: 我有一个 XML 文件,其数据如下: <Invoice xmlns='urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' xmlns:cac='urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' xmlns:cbc='urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' xmlns:ext='urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2'><ext:UBLExtensions> <ext:UBLExtension> <ext:ExtensionURI>urn:oasis:names:specification:ubl:dsig:enveloped:xades</ext:ExtensionURI> <ext:ExtensionContent> <sig:UBLDocumentSignatures xmlns:sig='urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2' xmlns:sac='urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2' xmlns:sbc='urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2'> <sac:SignatureInformation> <cbc:ID>urn:oasis:names:specification:ubl:signature:1</cbc:ID> <sbc:ReferencedSignatureID>urn:oasis:names:specification:ubl:signature:Invoice</sbc:ReferencedSignatureID> <ds:Signature xmlns:ds='http://www.w3.org/2000/09/xmldsig#' Id='signature'> <ds:SignedInfo> <ds:CanonicalizationMethod Algorithm='http://www.w3.org/2006/12/xml-c14n11'/> <ds:SignatureMethod Algorithm='http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256'/> <ds:Reference Id='invoiceSignedData' URI=''> <ds:Transforms> <ds:Transform Algorithm='http://www.w3.org/TR/1999/REC-xpath-19991116'> <ds:XPath>not(//ancestor-or-self::ext:UBLExtensions)</ds:XPath> </ds:Transform> <ds:Transform Algorithm='http://www.w3.org/TR/1999/REC-xpath-19991116'> <ds:XPath>not(//ancestor-or-self::cac:Signature)</ds:XPath> </ds:Transform> <ds:Transform Algorithm='http://www.w3.org/TR/1999/REC-xpath-19991116'> <ds:XPath>not(//ancestor-or-self::cac:AdditionalDocumentReference[cbc:ID='QR'])</ds:XPath> </ds:Transform> <ds:Transform Algorithm='http://www.w3.org/2006/12/xml-c14n11'/> </ds:Transforms> <ds:DigestMethod Algorithm='http://www.w3.org/2001/04/xmlenc#sha256'/> <ds:DigestValue>CAgICAgICAgPC9jYWM6SW52b2ljZUxpbmU+DQogICAgICAgICAgICAgICAgICAgICAgICAgICAgPC9JbnZvaWNlPg==</ds:DigestValue> </ds:Reference> <ds:Reference Type='http://www.w3.org/2000/09/xmldsig#SignatureProperties' URI='#xadesSignedProperties'> <ds:DigestMethod Algorithm='http://www.w3.org/2001/04/xmlenc#sha256'/> <ds:DigestValue>NjY5ZWIzMjg5ZjhlZWY3NjUyNTdkNzFmYmIzMWMwMmQ4OGVkMDgzZWIzMWQwMmYxNGQ4OTMxNDYyMDVlMDkzNA==</ds:DigestValue> </ds:Reference> </ds:SignedInfo> <ds:SignatureValue>MEYCIQCPhRxAvaN69JA9Ng+twhxmpSZ3aQMkOR0p1LKZGZfwtQIhAO6yPeKAGn9slyDkhkJVOt7HEG2jObxsSiGCUGQ0Mb1l</ds:SignatureValue> <ds:KeyInfo> <ds:X509Data> <ds:X509Certificate>yNhycQ3bNlLFdOPlqYT6RVQTWgnK1Gh0NHdcSY4PfC0CIQCSAthXvv7tetUL69Wjp8BxnLLMwerxZhBnewo/gF3EJA==</ds:X509Certificate> </ds:X509Data> </ds:KeyInfo> <ds:Object> <xades:QualifyingProperties xmlns:xades='http://uri.etsi.org/01903/v1.3.2#' Target='signature'> <xades:SignedProperties Id='xadesSignedProperties'> <xades:SignedSignatureProperties> <xades:SigningTime>2023-12-26T19:49:21Z</xades:SigningTime> <xades:SigningCertificate> <xades:Cert> <xades:CertDigest> <ds:DigestMethod Algorithm='http://www.w3.org/2001/04/xmlenc#sha256'/> <ds:DigestValue>YjcyNzI4NWZiN2FlNzI1NTQxOGIzZGY1ZWY4MDc2N2JkMDIyMDM5ZDU5YjJmNWRkOTQ5YTZkMDU2YjEzYzJkZA==</ds:DigestValue> </xades:CertDigest> <xades:IssuerSerial> <ds:X509IssuerName>CN=TSZEINVOICE-SubCA-1, DC=extgazt, DC=gov, DC=local</ds:X509IssuerName> <ds:X509SerialNumber>2475382886904809774818644480820936050208702411</ds:X509SerialNumber> </xades:IssuerSerial> </xades:Cert> </xades:SigningCertificate> </xades:SignedSignatureProperties> </xades:SignedProperties> </xades:QualifyingProperties> </ds:Object> </ds:Signature> </sac:SignatureInformation> </sig:UBLDocumentSignatures> </ext:ExtensionContent> </ext:UBLExtension> </ext:UBLExtensions> <cbc:ProfileID>reporting:1.0</cbc:ProfileID> <cbc:ID>3333</cbc:ID> <cbc:UUID>feab5f74-ca2d-4047-b421-15907e1ec212</cbc:UUID> <cbc:IssueDate>2023-07-06</cbc:IssueDate> <cbc:IssueTime>07:17:26</cbc:IssueTime> <cbc:InvoiceTypeCode name='0100000'>388</cbc:InvoiceTypeCode> <cbc:DocumentCurrencyCode>SAR</cbc:DocumentCurrencyCode> <cbc:TaxCurrencyCode>SAR</cbc:TaxCurrencyCode> <cac:AdditionalDocumentReference> <cbc:ID>ICV</cbc:ID> <cbc:UUID>23</cbc:UUID> </cac:AdditionalDocumentReference> <cac:AdditionalDocumentReference> <cbc:ID>PIH</cbc:ID> <cac:Attachment> <cbc:EmbeddedDocumentBinaryObject mimeCode='text/plain'>NWZlY2ViNjZmZmM4NmYzOGQ5NTI3ODZjNmQ2OTZjNzljMmRiYzIzOWRkNGU5MWI0NjcyOWQ3M2EyN2ZiNTdlOQ==</cbc:EmbeddedDocumentBinaryObject> </cac:Attachment> </cac:AdditionalDocumentReference> <cac:AdditionalDocumentReference> <cbc:ID>QR</cbc:ID> <cac:Attachment> <cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">SDFWEsdf234SDFWER</cbc:EmbeddedDocumentBinaryObject> </cac:Attachment> </cac:AdditionalDocumentReference> <cac:Signature> <cbc:ID>urn:oasis:names:specification:ubl:signature:Invoice</cbc:ID> <cbc:SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped:xades</cbc:SignatureMethod> </cac:Signature> <cac:AccountingSupplierParty> <cac:Party> <cac:PartyIdentification> <cbc:ID schemeID='CRN'></cbc:ID> </cac:PartyIdentification> <cac:PostalAddress> <cbc:StreetName>dsf</cbc:StreetName> <cbc:BuildingNumber>3453</cbc:BuildingNumber> <cbc:CitySubdivisionName>xcvcx</cbc:CitySubdivisionName> <cbc:CityName>xcvxc</cbc:CityName> <cbc:PostalZone>45354</cbc:PostalZone> <cac:Country> <cbc:IdentificationCode>SA</cbc:IdentificationCode> </cac:Country> </cac:PostalAddress> <cac:PartyTaxScheme> <cbc:CompanyID>311380797100003</cbc:CompanyID> <cac:TaxScheme> <cbc:ID>VAT</cbc:ID> </cac:TaxScheme> </cac:PartyTaxScheme> <cac:PartyLegalEntity> <cbc:RegistrationName>LAMARA SUPERMARKET</cbc:RegistrationName> </cac:PartyLegalEntity> </cac:Party> </cac:AccountingSupplierParty> <cac:AccountingCustomerParty> <cac:Party> <cac:PartyIdentification> <cbc:ID schemeID='NAT'></cbc:ID> </cac:PartyIdentification> <cac:PostalAddress> <cbc:StreetName>ddd</cbc:StreetName> <cbc:BuildingNumber></cbc:BuildingNumber> <cbc:CitySubdivisionName>sss</cbc:CitySubdivisionName> <cbc:CityName>vvv</cbc:CityName> <cbc:PostalZone>55443</cbc:PostalZone> <cac:Country> <cbc:IdentificationCode>SA</cbc:IdentificationCode> </cac:Country> </cac:PostalAddress> <cac:PartyTaxScheme> <cbc:CompanyID>311380797200003</cbc:CompanyID> <cac:TaxScheme> <cbc:ID>VAT</cbc:ID> </cac:TaxScheme> </cac:PartyTaxScheme> <cac:PartyLegalEntity> <cbc:RegistrationName>MESS EXPENSE</cbc:RegistrationName> </cac:PartyLegalEntity> </cac:Party> </cac:AccountingCustomerParty> <cac:Delivery> <cbc:ActualDeliveryDate>2023-07-06</cbc:ActualDeliveryDate> </cac:Delivery> <cac:PaymentMeans> <cbc:PaymentMeansCode>30</cbc:PaymentMeansCode> </cac:PaymentMeans> <cac:AllowanceCharge> <cbc:ChargeIndicator>false</cbc:ChargeIndicator> <cbc:AllowanceChargeReason>discount</cbc:AllowanceChargeReason> <cbc:Amount currencyID='SAR'>0.00</cbc:Amount> <cac:TaxCategory> <cbc:ID schemeID='UN/ECE 5305' schemeAgencyID='6'>S</cbc:ID> <cbc:Percent>15</cbc:Percent> <cac:TaxScheme> <cbc:ID schemeID='UN/ECE 5153' schemeAgencyID='6'>VAT</cbc:ID> </cac:TaxScheme> </cac:TaxCategory> </cac:AllowanceCharge> <cac:TaxTotal> <cbc:TaxAmount currencyID='SAR'>17.71</cbc:TaxAmount> </cac:TaxTotal> <cac:TaxTotal> <cbc:TaxAmount currencyID='SAR'>17.71</cbc:TaxAmount> <cac:TaxSubtotal> <cbc:TaxableAmount currencyID='SAR'>118.04</cbc:TaxableAmount> <cbc:TaxAmount currencyID='SAR'>17.71</cbc:TaxAmount> <cac:TaxCategory> <cbc:ID schemeID='UN/ECE 5305' schemeAgencyID='6'>S</cbc:ID> <cbc:Percent>15.00</cbc:Percent> <cac:TaxScheme> <cbc:ID schemeID='UN/ECE 5153' schemeAgencyID='6'>VAT</cbc:ID> </cac:TaxScheme> </cac:TaxCategory> </cac:TaxSubtotal> </cac:TaxTotal> <cac:LegalMonetaryTotal> <cbc:LineExtensionAmount currencyID='SAR'>118.04</cbc:LineExtensionAmount> <cbc:TaxExclusiveAmount currencyID='SAR'>118.04</cbc:TaxExclusiveAmount> <cbc:TaxInclusiveAmount currencyID='SAR'>135.75</cbc:TaxInclusiveAmount> <cbc:AllowanceTotalAmount currencyID='SAR'>0.00</cbc:AllowanceTotalAmount> <cbc:PrepaidAmount currencyID='SAR'>0.00</cbc:PrepaidAmount> <cbc:PayableAmount currencyID='SAR'>135.75</cbc:PayableAmount> </cac:LegalMonetaryTotal> <cac:InvoiceLine> <cbc:ID>1</cbc:ID> <cbc:InvoicedQuantity unitCode='PCE'>1.00</cbc:InvoicedQuantity> <cbc:LineExtensionAmount currencyID='SAR'>5.00</cbc:LineExtensionAmount> <cac:TaxTotal> <cbc:TaxAmount currencyID='SAR'>0.75</cbc:TaxAmount> <cbc:RoundingAmount currencyID='SAR'>5.75</cbc:RoundingAmount> </cac:TaxTotal> <cac:Item> <cbc:Name>sdfds</cbc:Name> <cac:ClassifiedTaxCategory> <cbc:ID>S</cbc:ID> <cbc:Percent>15</cbc:Percent> <cac:TaxScheme> <cbc:ID>VAT</cbc:ID> </cac:TaxScheme> </cac:ClassifiedTaxCategory> </cac:Item> <cac:Price> <cbc:PriceAmount currencyID='SAR'>5.00</cbc:PriceAmount> <cac:AllowanceCharge> <cbc:ChargeIndicator>false</cbc:ChargeIndicator> <cbc:AllowanceChargeReason>discount</cbc:AllowanceChargeReason> <cbc:Amount currencyID='SAR'>0.00</cbc:Amount> </cac:AllowanceCharge> </cac:Price> </cac:InvoiceLine><cac:InvoiceLine> <cbc:ID>2</cbc:ID> <cbc:InvoicedQuantity unitCode='PCE'>1.00</cbc:InvoicedQuantity> <cbc:LineExtensionAmount currencyID='SAR'>113.04</cbc:LineExtensionAmount> <cac:TaxTotal> <cbc:TaxAmount currencyID='SAR'>16.96</cbc:TaxAmount> <cbc:RoundingAmount currencyID='SAR'>130.00</cbc:RoundingAmount> </cac:TaxTotal> <cac:Item> <cbc:Name>eeee</cbc:Name> <cac:ClassifiedTaxCategory> <cbc:ID>S</cbc:ID> <cbc:Percent>15</cbc:Percent> <cac:TaxScheme> <cbc:ID>VAT</cbc:ID> </cac:TaxScheme> </cac:ClassifiedTaxCategory> </cac:Item> <cac:Price> <cbc:PriceAmount currencyID='SAR'>113.04</cbc:PriceAmount> <cac:AllowanceCharge> <cbc:ChargeIndicator>false</cbc:ChargeIndicator> <cbc:AllowanceChargeReason>discount</cbc:AllowanceChargeReason> <cbc:Amount currencyID='SAR'>0.00</cbc:Amount> </cac:AllowanceCharge> </cac:Price> </cac:InvoiceLine> </Invoice> 从这个 XML 我想更新 XML 元素的值: <cac:AdditionalDocumentReference> <cbc:ID>QR</cbc:ID> <cac:Attachment> <cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">SDFWEsdf234SDFWER</cbc:EmbeddedDocumentBinaryObject> </cac:Attachment> </cac:AdditionalDocumentReference> 我想用 C# 更改 EmbeddedDocumentBinaryObject 的值。 我尝试过的: XDocument xdoc = XDocument.Load(Server.MapPath("~/XML/EInvoice.xml")); XNamespace ns = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"; xdoc.Descendants(ns + "ProfileID").FirstOrDefault()?.SetValue("sdfwer"); 通过上面的代码,我可以更新 ProfileID 元素的值。有什么办法可以用这个方法来达到我上面的要求。我尝试了之前的不同问题,但无法获得确切的选项。我是处理 XML 的初学者。 请尝试以下解决方案。 c# void Main() { const string FILENAME = @"e:\Temp\CodinozTechnologies.xml"; const string NEWFILENAME = @"e:\Temp\CodinozTechnologies_New.xml"; const string FINDVALUE = "QR"; const string NEWVALUE = "++NewValue++"; XDocument xdoc = XDocument.Load(FILENAME); XNamespace ns = xdoc.Root.GetNamespaceOfPrefix("cbc"); xdoc.Descendants(ns + "EmbeddedDocumentBinaryObject") .Where(x => x.Ancestors().Elements(ns + "ID").FirstOrDefault()?.Value == FINDVALUE) .FirstOrDefault()? .SetValue(NEWVALUE); xdoc.Save(NEWFILENAME); }

回答 1 投票 0

使用 LINQ To XML 读取多行文本属性时保留换行符

使用 LINQ To XML (System.Xml.Linq) 读取 XML 文件时,我需要能够保留多行文本属性中的换行符。下面是一个 XML 文件示例。 使用 LINQ To XML (System.Xml.Linq) 读取 XML 文件时,我需要能够保留多行文本属性中的换行符。这是一个 XML 文件示例。 <?xml version="1.0" encoding="utf-8"?> <TestRoot> <Properties Description="Line 1 Line 2" /> </TestRoot> 这是我尝试过的代码: const string xmlPath = @"C:\Blah\Test.xml"; var rootElement = XElement.Load(xmlPath); var propertiesElement = rootElement.Element("Properties")!; var descriptionAttribute = propertiesElement.Attribute("Description")!; string description = descriptionAttribute.Value; Console.WriteLine(description); 预期输出: Line 1 Line 2 实际产量: Line 1 Line 2 LINQ to XML(以及内部的XmlReader)正在用空格替换属性内的换行符,因为 XML 标准要求它们这样做。来自 3.3.3 属性值标准化: 3.3.3 属性值标准化 在将属性值传递给应用程序或检查有效性之前,XML 处理器必须通过应用下面的算法或使用某种其他方法来标准化属性值,以便传递给应用程序的值与传递给应用程序的值相同由算法产生。 所有换行符必须在 #xA 的输入上进行标准化,如 2.11 行尾处理中所述,因此该算法的其余部分对以这种方式标准化的文本进行操作。 以由空字符串组成的标准化值开始。 对于非标准化属性值中的每个字符、实体引用或字符引用,从第一个开始一直到最后一个,执行以下操作: 对于字符引用,将引用的字符附加到标准化值。 对于实体引用,递归地将此算法的步骤 3 应用于实体的替换文本。 对于空白字符(#x20、#xD、#xA、#x9),将空格字符 (#x20) 附加到标准化值。 对于另一个字符,将该字符附加到标准化值。 因此,如果您希望 XML 属性值中包含换行符,则必须将其转义为 &#10。事实上,它没有如此转义将是发送系统中的一个错误,应该修复。 如果出于某种原因您无法修复发送系统,则旧的 XmlTextReader 类有一个可以禁用的 Normalization 属性: 获取或设置一个值,指示是否规范化空白和属性值。 因此可以使用以下代码读取您的属性值: using var xmlReader = new XmlTextReader(xmlPath) { Normalization = false, }; var rootElement = XElement.Load(xmlReader); var propertiesElement = rootElement.Element("Properties")!; var descriptionAttribute = propertiesElement.Attribute("Description")!; string description = descriptionAttribute.Value; Console.WriteLine(description); 请注意以下XmlTextReader文档备注: 从 .NET Framework 2.0 开始,我们建议您使用 XmlReader.Create 方法创建 XmlReader 实例以利用新功能。 因此使用需要您自担风险。 演示小提琴在这里。

回答 1 投票 0

无法让 XDocument/XmlWriter 编写带有缩进和换行的 xml 文件(使用下面的 XmlProvider)

这是代码 (xml 文档本身是通过使用 FSharp.Data 中的 F# XmlProvider 读取 xml 创建的,然后通过其下属 XDocument 对其进行操作) 静态无效保存(XDocument ...

回答 1 投票 0

C# 如何替换两个 Xml 元素值?

起始代码: var xDocument = XDocument.Parse(rawXmlString); 想象一下,我已将下面的 XML 加载到 XDocument 中,并且我想使用 Linq-to-XML: 起始代码: var xDocument = XDocument.Parse(rawXmlString); 想象一下,我已将下面的 XML 加载到 XDocument 中,并且我想使用 Linq-to-XML: <ParameterValues> <ParameterValue> <Name>Head Coach</Name> <Value>Bill Belichick</Value> </ParameterValue> <ParameterValue> <Name>Quarterback</Name> <Value>Mac Jones</Value> </ParameterValue> </ParameterValues> 使用 C#,分别关闭 Name 值 Head Coach 和 Quarterback,如何替换: Bill Belichick 与 Mike Vrabel? Mac Jones 与 Kirk Cousins? 另外,如果有帮助的话,这个 XML 文档将只存在于内存中。它 不会被持久化到文件中。 ParameterValues 确实会是 根节点。 ParameterValue节点可能会出现故障,有一天 可能有两个以上的子节点。 我尝试过的: var xDocument = XDocument.Parse(rawXmlString); 我已经尝试过各种 Linq-to-XML 查询方法,但到目前为止运气不佳。 这里是通过 LINQ to XML 实现的。 c# void Main() { const string NEWHEADCOACH = "Mike Vrabel"; const string NEWQUARTERBACK = "Kirk Cousins"; XDocument xdoc = XDocument.Parse(@"<ParameterValues> <ParameterValue> <Name>Head Coach</Name> <Value>Bill Belichick</Value> </ParameterValue> <ParameterValue> <Name>Quarterback</Name> <Value>Mac Jones</Value> </ParameterValue> </ParameterValues>"); xdoc.Descendants("ParameterValue") .Where(x => x.Element("Name").Value == "Head Coach") .Elements("Value").FirstOrDefault()?.SetValue(NEWHEADCOACH); xdoc.Descendants("ParameterValue") .Where(x => x.Element("Name").Value == "Quarterback") .Elements("Value").FirstOrDefault()?.SetValue(NEWQUARTERBACK); Console.WriteLine(xdoc); } 输出 <ParameterValues> <ParameterValue> <Name>Head Coach</Name> <Value>Mike Vrabel</Value> </ParameterValue> <ParameterValue> <Name>Quarterback</Name> <Value>Kirk Cousins</Value> </ParameterValue> </ParameterValues>

回答 1 投票 0

与 LINQ to XML 联合

我需要将两组 XElement 合并为一组唯一的元素。使用 .Union() 扩展方法,我只得到一个“union all”而不是联合。我错过了什么吗? 变种

回答 4 投票 0

获取 XElement 在原始文档中的位置和长度

我正在解析特定节点的 XML 文档,并希望稍后在 ui 中显示 xml 文档,突出显示特定部分。为此,我需要知道元素的位置...

回答 2 投票 0

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