我想选择并更改c#中以下xml的节点的值。这是一个ubl-tr xml示例。假设我要更改cbc:UUID的值。
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="general.xslt"?>
<Invoice xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../xsdrt/maindoc/UBL-Invoice-2.1.xsd"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:n4="http://www.altova.com/samplexml/other-namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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:ExtensionContent>
<n4:auto-generated_for_wildcard/>
</ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions>
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>TR1.2</cbc:CustomizationID>
<cbc:ProfileID>TEMELFATURA</cbc:ProfileID>
<cbc:ID>GIB20090000000001</cbc:ID>
<cbc:CopyIndicator>false</cbc:CopyIndicator>
<cbc:UUID>F47AC10B-58CC-4372-A567-0E02B2C3D479</cbc:UUID>
<cbc:IssueDate>2009-01-05</cbc:IssueDate>
<cbc:IssueTime>14:42:00</cbc:IssueTime>
<cbc:InvoiceTypeCode>SATIS</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>TRY</cbc:DocumentCurrencyCode>
<cbc:LineCountNumeric>1</cbc:LineCountNumeric>
</Invoice>
答案 0 :(得分:0)
这是使用XML Linq的解决方案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement UUID = doc.Descendants().Where(x => x.Name.LocalName == "UUID").FirstOrDefault();
UUID.SetValue("new value");
}
}
}
答案 1 :(得分:0)
您可以使用UblTr NuGet package反序列化为对象开具发票并更改值
XmlSerializer serializer = new XmlSerializer(typeof(UblTr.MainDoc.InvoiceType));
using (StreamReader reader = new StreamReader(path))
{
var invoice = (UblTr.MainDoc.InvoiceType)serializer.Deserialize(reader);
invoice.UUID.Value = "new uuid";
}