VB.NET使用新节更新现有XML
我在一个vb.net项目中工作,我想用一个新的部分更新现有的XMLVB.NET使用新节更新现有XML,xml,vb.net,Xml,Vb.net,我在一个vb.net项目中工作,我想用一个新的部分更新现有的XML <BookUpdateable Action="Renew"> <BookOption>Book</BookOption> <BookMaster Action="None"> <Key> <MasterReference>7678812382</MasterReference>
<BookUpdateable Action="Renew">
<BookOption>Book</BookOption>
<BookMaster Action="None">
<Key>
<MasterReference>7678812382</MasterReference>
</Key>
<Assured Action="Update" xsi:type="OrgDataUpdateable">
<Key>
<CaptionCode>BOOKINTRO</CaptionCode>
<PrevTransaction>0</PrevTransaction>
</Key>
<Addresses>
<IsDeleted>true</IsDeleted>
</Addresses>
</Assured>
<Units>
<IsDeleted>true</IsDeleted>
</Units>
<Sections>
<SectionDataUpdateable Action="Update" SectionTypeCode="NEW" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>10</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
/*************************************************
NEW SECTION TO BE ADDED HERE
*************************************************/
</Sections>
<BookDataExts>
<MasterStatusCode>BOOKEXTN</MasterStatusCode>
<EffectiveDate>2017-07-28</EffectiveDate>
</BookDataExts>
</BookMaster>
<ReturnFields>false</ReturnFields>
</BookUpdateable>
我需要像这样在sections元素下插入另一个节
<SectionDataUpdateable Action="Update" SectionTypeCode="OLD" SubSectionTypeCode="TWO">
<Key>
<SectionSequence>05</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
05
真的
在您的示例中,文本“NEW SECTION TO ADDED HERE”的位置表明您希望将新数据添加为第一个
的最后一个同级。因此,我使用了以下示例XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<BookUpdateable Action="Renew">
<BookOption>Book</BookOption>
<BookMaster Action="None">
<Key>
<MasterReference>7678812382</MasterReference>
</Key>
<Assured Action="Update" xsi:type="OrgDataUpdateable">
<Key>
<CaptionCode>BOOKINTRO</CaptionCode>
<PrevTransaction>0</PrevTransaction>
</Key>
<Addresses>
<IsDeleted>true</IsDeleted>
</Addresses>
</Assured>
<Units>
<IsDeleted>true</IsDeleted>
</Units>
<Sections>
<SectionDataUpdateable Action="Update" SectionTypeCode="NEW" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>10</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
<SectionDataUpdateable Action="Nothing" SectionTypeCode="TEST" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>99</SectionSequence>
</Key>
<IsDeleted>dontcare</IsDeleted>
</SectionDataUpdateable>
<!--
/*********************************************
NEW SECTION TO BE INSERTED BEFORE THIS COMMENT
*********************************************/
-->
</Sections>
<BookDataExts>
<MasterStatusCode>BOOKEXTN</MasterStatusCode>
<EffectiveDate>2017-07-28</EffectiveDate>
</BookDataExts>
</BookMaster>
<ReturnFields>false</ReturnFields>
</BookUpdateable>
书
7678812382
考虑一下,这是一种专用语言,用于转换XML文件,如更新部分所需。与大多数通用语言(包括Java、Python、C#、PHP)一样,VB.Net可以在各种库中运行XSLT1.0脚本。这里不需要循环、解析或条件逻辑
众所周知,我在多个编程线程中提倡XSLT。所以,虽然我对VB.Net一无所知,但我确实包含了下面提供的MSDN源代码。请根据需要调整
XSLT(另存为.xsl文件,它是一个格式良好的.xml文件,可以从文件或字符串加载)
XML输出(输入中添加了xmlns:xsi=“www.example.com”声明)
书
7678812382
图书介绍
0
真的
真的
10
真的
05
真的
书展
2017-07-28
假的
嗨,我试过这个。它工作得很好。但我面临着一个问题。xmlns=”“99 dontcare@user1508967附带的新部分“xmlns=”“
部分可能与您未显示的代码有关-注意,我删除了“r:”部分(c.f.nameCodeSections=xDoc.SelectSingleNode(“/r:BookUpdateable[1]/r:BookMaster”,xNsMgr)
,因为问题中没有定义。您的XML示例缺少命名空间声明,xmlns:xsi=…
,因此格式不正确,解析失败。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<BookUpdateable Action="Renew">
<BookOption>Book</BookOption>
<BookMaster Action="None">
<Key>
<MasterReference>7678812382</MasterReference>
</Key>
<Assured Action="Update" xsi:type="OrgDataUpdateable">
<Key>
<CaptionCode>BOOKINTRO</CaptionCode>
<PrevTransaction>0</PrevTransaction>
</Key>
<Addresses>
<IsDeleted>true</IsDeleted>
</Addresses>
</Assured>
<Units>
<IsDeleted>true</IsDeleted>
</Units>
<Sections>
<SectionDataUpdateable Action="Update" SectionTypeCode="NEW" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>10</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
<SectionDataUpdateable Action="Nothing" SectionTypeCode="TEST" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>99</SectionSequence>
</Key>
<IsDeleted>dontcare</IsDeleted>
</SectionDataUpdateable>
<!--
/*********************************************
NEW SECTION TO BE INSERTED BEFORE THIS COMMENT
*********************************************/
-->
</Sections>
<BookDataExts>
<MasterStatusCode>BOOKEXTN</MasterStatusCode>
<EffectiveDate>2017-07-28</EffectiveDate>
</BookDataExts>
</BookMaster>
<ReturnFields>false</ReturnFields>
</BookUpdateable>
Option Infer On
Option Strict On
Imports System.Xml
Module Module1
Sub Main()
Dim src = "C:\temp\BookUpdateable.xml"
Dim dest = "C:\temp\BookUpdateable2.xml"
Dim xmlDoc As New XmlDocument
Dim settings = New XmlReaderSettings With {.NameTable = New NameTable()}
Dim nsm = New XmlNamespaceManager(settings.NameTable)
nsm.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
Dim context = New XmlParserContext(Nothing, nsm, "", XmlSpace.Default)
Dim reader = XmlReader.Create(src, settings, context)
xmlDoc.Load(reader)
Dim nameCodeSections = xmlDoc.SelectNodes("/BookUpdateable[1]/BookMaster/Sections/SectionDataUpdateable", nsm)
Dim newData As XElement = <SectionDataUpdateable Action="Update" SectionTypeCode="OLD" SubSectionTypeCode="TWO">
<Key>
<SectionSequence>05</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
Dim xd = New XmlDocument()
xd.LoadXml(newData.ToString())
nameCodeSections(0).ParentNode.InsertAfter(xmlDoc.ImportNode(xd.FirstChild, True), nameCodeSections(nameCodeSections.Count - 1))
xmlDoc.Save(dest)
Console.WriteLine("Done.")
Console.ReadLine()
End Sub
End Module
<?xml version="1.0" encoding="utf-8"?>
<BookUpdateable Action="Renew">
<BookOption>Book</BookOption>
<BookMaster Action="None">
<Key>
<MasterReference>7678812382</MasterReference>
</Key>
<Assured Action="Update" xsi:type="OrgDataUpdateable" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Key>
<CaptionCode>BOOKINTRO</CaptionCode>
<PrevTransaction>0</PrevTransaction>
</Key>
<Addresses>
<IsDeleted>true</IsDeleted>
</Addresses>
</Assured>
<Units>
<IsDeleted>true</IsDeleted>
</Units>
<Sections>
<SectionDataUpdateable Action="Update" SectionTypeCode="NEW" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>10</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
<SectionDataUpdateable Action="Nothing" SectionTypeCode="TEST" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>99</SectionSequence>
</Key>
<IsDeleted>dontcare</IsDeleted>
</SectionDataUpdateable>
<SectionDataUpdateable Action="Update" SectionTypeCode="OLD" SubSectionTypeCode="TWO">
<Key>
<SectionSequence>05</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
<!--
/*********************************************
NEW SECTION TO BE INSERTED BEFORE THIS COMMENT
*********************************************/
-->
</Sections>
<BookDataExts>
<MasterStatusCode>BOOKEXTN</MasterStatusCode>
<EffectiveDate>2017-07-28</EffectiveDate>
</BookDataExts>
</BookMaster>
<ReturnFields>false</ReturnFields>
</BookUpdateable>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Identity Transform -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Add New SectiondataUdpateable -->
<xsl:template match="Sections">
<xsl:copy>
<xsl:copy-of select="*"/>
<SectionDataUpdateable Action="Update" SectionTypeCode="OLD" SubSectionTypeCode="TWO">
<Key>
<SectionSequence>05</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<BookUpdateable xmlns:xsi="www.example.com" Action="Renew">
<BookOption>Book</BookOption>
<BookMaster Action="None">
<Key>
<MasterReference>7678812382</MasterReference>
</Key>
<Assured Action="Update" xsi:type="OrgDataUpdateable">
<Key>
<CaptionCode>BOOKINTRO</CaptionCode>
<PrevTransaction>0</PrevTransaction>
</Key>
<Addresses>
<IsDeleted>true</IsDeleted>
</Addresses>
</Assured>
<Units>
<IsDeleted>true</IsDeleted>
</Units>
<Sections>
<SectionDataUpdateable Action="Update" SectionTypeCode="NEW" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>10</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
<SectionDataUpdateable Action="Update" SectionTypeCode="OLD" SubSectionTypeCode="TWO">
<Key>
<SectionSequence>05</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
</Sections>
<BookDataExts>
<MasterStatusCode>BOOKEXTN</MasterStatusCode>
<EffectiveDate>2017-07-28</EffectiveDate>
</BookDataExts>
</BookMaster>
<ReturnFields>false</ReturnFields>
</BookUpdateable>