简体   繁体   English

更改XML文件时出错

[英]Error while changing XML file

I am trying to change a XML file in code. 我正在尝试更改代码中的XML文件。 I have found some good examples online and tried to do the same but it doesn't work. 我在网上找到了一些很好的例子,并试图做同样的事情,但是它不起作用。 When I try my code with a simple XML document there are no errors but with my XML document I get this error : 当我使用简单的XML文档尝试代码时,没有错误,但是使用XML文档却出现以下错误:

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

This is my XML file : 这是我的XML文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<ns2:VATConsignment xmlns="http://www.minfin.fgov.be/InputCommon" xmlns:ns2="http://www.minfin.fgov.be/VATConsignment" VATDeclarationsNbr="@aantal_aangiftes@">
    <ns2:VATDeclaration SequenceNumber="@aangifte_nr@">
<ns2:Declarant>
            <VATNumber>@firma_btw@</VATNumber>
            <Name>@firma_naam@</Name>
            <Street>@firma_straat@</Street>
            <PostCode>@firma_pnr@</PostCode>
            <City>@firma_wpl@</City>
            <CountryCode>BE</CountryCode>
            <EmailAddress>@firma_email@</EmailAddress>
            <Phone>@firma_tel@</Phone>
        </ns2:Declarant>
<ns2:Period>
            <ns2:Month>@maand@</ns2:Month>
            <ns2:Quarter>@kwart@</ns2:Quarter>
            <ns2:Year>@jaar@</ns2:Year>
        </ns2:Period>
<ns2:Data>
            <ns2:Amount GridNumber="0">@VAK0@</ns2:Amount>
            <ns2:Amount GridNumber="1">@VAK1@</ns2:Amount>
            <ns2:Amount GridNumber="2">@VAK2@</ns2:Amount>
            <ns2:Amount GridNumber="3">@VAK3@</ns2:Amount>

            <ns2:Amount GridNumber="44">@VAK44@</ns2:Amount>
            <ns2:Amount GridNumber="45">@VAK45@</ns2:Amount>
            <ns2:Amount GridNumber="46">@VAK46@</ns2:Amount>
            <ns2:Amount GridNumber="47">@VAK47@</ns2:Amount>
            <ns2:Amount GridNumber="48">@VAK48@</ns2:Amount>
            <ns2:Amount GridNumber="49">@VAK49@</ns2:Amount>

            <ns2:Amount GridNumber="54">@VAK54@</ns2:Amount>
            <ns2:Amount GridNumber="55">@VAK55@</ns2:Amount>
            <ns2:Amount GridNumber="56">@VAK56@</ns2:Amount>
            <ns2:Amount GridNumber="57">@VAK57@</ns2:Amount>
            <ns2:Amount GridNumber="59">@VAK59@</ns2:Amount>

            <ns2:Amount GridNumber="61">@VAK61@</ns2:Amount>
            <ns2:Amount GridNumber="62">@VAK62@</ns2:Amount>
            <ns2:Amount GridNumber="63">@VAK63@</ns2:Amount>
            <ns2:Amount GridNumber="64">@VAK64@</ns2:Amount>
            <ns2:Amount GridNumber="65">@VAK65@</ns2:Amount>
            <ns2:Amount GridNumber="66">@VAK66@</ns2:Amount>

            <ns2:Amount GridNumber="71">@VAK71@</ns2:Amount>
            <ns2:Amount GridNumber="72">@VAK72@</ns2:Amount>

            <ns2:Amount GridNumber="81">@VAK81@</ns2:Amount>
            <ns2:Amount GridNumber="82">@VAK82@</ns2:Amount>
            <ns2:Amount GridNumber="83">@VAK83@</ns2:Amount>
            <ns2:Amount GridNumber="84">@VAK84@</ns2:Amount>
            <ns2:Amount GridNumber="85">@VAK85@</ns2:Amount>
            <ns2:Amount GridNumber="86">@VAK86@</ns2:Amount>
            <ns2:Amount GridNumber="87">@VAK87@</ns2:Amount>
            <ns2:Amount GridNumber="88">@VAK88@</ns2:Amount>
            <ns2:Amount GridNumber="91">@VAK91@</ns2:Amount>
        </ns2:Data>
<ns2:ClientListingNihil>@nihil_listing@</ns2:ClientListingNihil>
        <ns2:Ask Payment="NO" Restitution="NO"/>
        <ns2:Comment>@commentaar@</ns2:Comment>
    </ns2:VATDeclaration>
</ns2:VATConsignment>    

And this is my code i have written : 这是我写的代码:

        XmlDocument doc2 = new XmlDocument();
        doc2.Load(@"C:\Users\Kevin\Desktop\xsd\TemplateAangifte.xml");

        XmlNode aangeverNode = doc2.SelectSingleNode(@"/ns2:VATConsignment/ns2:VATDeclaration/ns2:Declarant");

The error gets triggered when trying to load the XmlNode . 尝试加载XmlNode时触发错误。 I am pretty new to the XML story so every help is welcome ! 我对XML的故事还很陌生,因此欢迎大家的帮助!

Thanks ! 谢谢 !

Kevin 凯文

You need to set up the namespace manager mapping each namespace that is declared in your XML source file and that you use in your XPath expression with a prefix. 您需要设置命名空间管理器,以映射XML源文件中声明的每个命名空间以及在XPath表达式中使用的每个前缀的前缀。 The prefix that you declare in the mapping should be used in your XPath selectors. 您在映射中声明的前缀应在XPath选择器中使用。 The prefix need not be the same as the one used in the source file (it may be the same as the original prefix, but it doesn't have to be). 前缀不必与源文件中使用的前缀相同(它可以与原始前缀相同,但不必相同)。

Here are the declarations you need to add if you want to access any element from your source file: 如果要访问源文件中的任何元素 ,则需要添加以下声明:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc2.NameTable);
nsmgr.AddNamespace("ns2", "http://www.minfin.fgov.be/VATConsignment");

// Not necessary unless you decide to use an unprefixed element from your source
nsmgr.AddNamespace("ns0", "http://www.minfin.fgov.be/InputCommon"); 

To access an unprefixed element, you would use ns0:EmailAddress for example. 要访问未添加前缀的元素,请使用ns0:EmailAddress例如。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM