我正在使用 XmlProvider,并且我正試圖對一個 xml 檔案進行一些更改。
我的問題是在向檔案寫入時,命名空間的格式發生了變化。我已經嘗試了兩種方法,其中第一種方法為我提供了正確的格式,但我無法將其用于更復雜的更新,例如用新的發票線替換所有發票行。
我怎樣才能更新/保存,使其格式與給供應商的xml一樣?
常見的代碼:
打開系統
打開System.Xml.Linq
open FSharp.Data
let cbc = XNamespace.Get("urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" )
let cac = XNamespace.Get("urn:oasis:names:specification:ubl:schema:xsd:CommonAggateComponents-2")
type Invoice = XmlProvider<"""
<ehf:Invoice xmlns:cbc="urn:oasis:names:specification: ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd: CommonAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd: CommonExtensionComponents-2" xmlns:ehf="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">/span>
<cbc:ID>5584-2021-2</cbc:ID>
<cac:InvoiceLine>/span>
<cbc:ID>1a</cbc:ID>
<cbc:Note>/span>Note</cbc:Note>
<cac:OrderLineReference>/span>
<cbc:LineID>1</cbc:LineID>/span>
</cac:OrderLineReference>/span>
<cac:Item>/span>
<cbc:Name>Item 1</cbc:Name>
</cac:Item>/span>
</cac:InvoiceLine>/span>
<cac:InvoiceLine>/span>
<cbc:ID>1b</cbc:ID>
<cbc:Note>/span>Note</cbc:Note>
<cac:OrderLineReference>/span>
<cbc:LineID>2</cbc:LineID>
</cac:OrderLineReference>/span>
<cac:Item>/span>
<cbc:Name>Item 2</cbc:Name>
</cac:Item>/span>
</cac:InvoiceLine>/span>
</ehf:Invoice>/span>
"">。
第一種方法(結果看起來像我想要的那樣):
invoice.XElement.SetElementValue(cbc "ID", "New id")
printfn $"{invoice |> string}"
//<?xml version="1.0" encoding="utf-8"? >
//<ehf:Invoice xmlns:cbc="urn:oasis:names: specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema: xsd:CommonAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd: CommonExtensionComponents-2" xmlns:ehf="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"> // "urn:oasis:names:specification:ubl:schema:xsd:Invoice"
// <cbc:ID>新 id</cbc:ID>
// <cac:InvoiceLine>
// <cbc:ID>1a</cbc:ID>
// <cbc:Note>Note</cbc:Note>
// <cac:OrderLineReference>
// <cbc:LineID>1</cbc:LineID>
// </cac:OrderLineReference>
// <cac:Item>
// <cbc:Name>Item 1</cbc:Name>
// </cac:Item>/span>
// </cac:InvoiceLine>
// <cac:InvoiceLine>
// <cbc:ID>1b</cbc:ID>
// <cbc:Note>Note</cbc:Note>
// <cac:OrderLineReference>
// <cbc:LineID>2</cbc:LineID>
// </cac:OrderLineReference>
// <cac:Item>
// <cbc:Name>Item 2</cbc:Name>
// </cac:Item>/span>
// </cac:InvoiceLine>
//</ehf:Invoice>/span>
第二種方法(給我錯誤的命名空間格式):
let line = invoice.InvoiceLines.[0] 。
讓newLine = Invoice.InvoiceLine(
"新ID"。
line.Note,
line.OrderLineReference,
line.Item
)
let changedInvoice = Invoice.Invoice(
invoice.Id,
[|newLine|]
)
printfn $"{changedInvoice |> string}"
//<?xml version="1.0" encoding="utf-8"? >
//<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
// <ID xmlns="urn:oasis:names: specification:ubl:schema:xsd:CommonBasicComponents-2">新 id</ID>
// <InvoiceLine xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"> // <
// <ID xmlns="urn:oasis:names: specification:ubl:schema:xsd:CommonBasicComponents-2">新 id</ID>
// <Note xmlns="urn:oasis:names: specification:ubl:schema:xsd:CommonBasicComponents-2">Note</Note>
// <OrderLineReference>
// <LineID xmlns="urn:oasis:names: specification:ubl:schema:xsd:CommonBasicComponents-2">1</LineID>/span>
// </OrderLineReference>/span>
// <Item>
// <Name xmlns="urn:oasis:names: specification:ubl:schema:xsd:CommonBasicComponents-2">Item 1</Name>
// </Item>
// </InvoiceLine>/span>
//</Invoice>/span>
uj5u.com熱心網友回復:
問題是,新創建的更改后的發票沒有頂層的xmlns屬性,這些屬性在原始檔案中,并定義了命名空間前綴。你可以通過在創建新的changedInvoice之后添加這些屬性來解決這個問題:
let cbc = XNamespace.Get "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
let cac = XNamespace.Get "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
let ext = XNamespace.Get "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
let ehf = XNamespace.Get "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
changedInvoice.XElement.SetAttributeValue(XNamespace.Xmlns "cbc", cbc)
changedInvoice.XElement.SetAttributeValue(XNamespace.Xmlns "cac", cac)
changedInvoice.XElement.SetAttributeValue(XNamespace.Xmlns "ext", ext)
changedInvoice.XElement.SetAttributeValue(XNamespace.Xmlns "ehf", ehf)
changedInvoice.XElement.ToString()
或者,也許一個更簡單的方法是從原始發票中復制這些內容:
for a in invoice.XElement.Attributes() do
changedInvoice.XElement.SetAttributeValue(a。 Name, a.Value)
changedInvoice.XElement.ToString()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/324050.html
標籤:
