某些根信息被视为非法字符 [英] Some root information is seen as illegal characters

查看:81
本文介绍了某些根信息被视为非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XmlDocument读取XML文件并使用XPath来访问我的子节点。我添加了一个NamespaceManager并指向我的parentNode。目的是查找具有特定名称的子节点,并更改innertext,无论childnode是否具有属性。它还需要是childnode中的特定单词,而不是单词的一部分。我怎么能这样做?



例如:



I'm trying to read the XML file with XmlDocument and using XPath to get to my childnodes. I've added a NamespaceManager and pointed to my parentNode. The purpose is to find childnodes with specific names and change the innertext whether or not the childnode has an attribute. Also it needs to be that specific word in the childnode, not a part of a word. How could I do this please?

example:

<?xml version="1.0"?>
<AnXMLTestFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="un:unece:260:data:EEM:02-02-AnXMLTestFile">
  <HeaderBEDocument>
    <Identification>45071dc8-558d-439a-8f0a-88ae73a74910</Identification>
    <DocumentType listAgencyIdentifier="6">386</DocumentType>
    <Creation>2016-06-14T12:31:58.0+01:00</Creation>
  </HeaderBEDocumtent>
  <PayloadBEInvoiceEvent>
    <Identification>45071dc8-558d-439a-8f0a-88ae73a74910</Identification>
    <Function listAgencyIdentifier="6">9</Function>
    <CalculationDateOccurrence>2016-06-14T12:31:58.0+01:00</CalculationDateOccurrence>
    <ConsumptionMonthOccurrence>--04</ConsumptionMonthOccurrence>
    <ConsumptionYearOccurrence>2016</ConsumptionYearOccurrence>
    <SDPUsedServiceDeliveryPointLocation>
      <HeadpointIdentificationDomainLocation>
        <Identification schemeAgencyIdentifier="9">941449900000000028</Identification>
      </HeadpointIdentificationDomainLocation>
      <SCI schemeAgencyIdentifier="86">SC_COMMINJ</SCI>
    </SDPUsedServiceDeliveryPointLocation>
  </PayloadBEInvoiceEvent>





以粗体显示的身份证明应更改innertext。



在调试时我收到'发现非法字符'错误。如果我遗漏了根节点的第一部分,则不会再显示错误消息,并且innertext会按原样更改。我该如何解决这个问题?



我的尝试:



从根中省略的字符:



The 'Identification' in bold should change of innertext.

On debugging I get the 'illegal characters were found' error. If I leave out the first part of my root node no error message are shown anymore and the innertext changes as it should. How can I fix this please?

What I have tried:

characters left out from the root:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"





Innertext填写调试信息。但我需要那些URL!它们在我要更改的所有150个Xml文件中。



Innertext filling up on debugging. But I need those URL's! They are in all 150 Xml-files that I want to change.

推荐答案

您可以使用 XmlTextReader 来获取以顺序方式对所有元素进行处理。

更多信息: XmlTextReader.Read方法(System.Xml) [ ^ ]

XmlTextReader - 初学者指南 [ ^ ]
You can use XmlTextReader to get to all elements in a sequential way.
More information: XmlTextReader.Read Method (System.Xml)[^]
The XmlTextReader - A Beginner's Guide[^]


解决:Foreach-loop包含我需要的不同节点改变innertext。在每个var XXXNode = node.SelectSingleNode(x:XXX,nsmgr)中;您需要在x之前添加//。像这样



Solved: Foreach-loop containes the different nodes I need to change innertext from. In each " var XXXNode = node.SelectSingleNode("x:XXX", nsmgr); " You need to add "//" before the "x". Like this

// Select Identification node from all nodes
                var identificationNode = node.SelectSingleNode("//x:Identification", nsmgr); //<==
                if (identificationNode != null)
                {
                    // Change value of indentification node to string
                    identificationNode.InnerText = "string";
                }
// Select SCI node from all parentnodes
                var SCI = node.SelectSingleNode("//x:SCI", nsmgr);//<==
                if (SCI != null)
                {
                    // change value of SCI node to string
                    SCI.InnerText = "56987465";
                }
                // Select ReferenceType node from all nodes
                var ReferenceType = node.SelectSingleNode("//x:ReferenceType", nsmgr);//<==
                if (ReferenceType != null)
                {
                    // change value of ReferenceType node to string
                    ReferenceType.InnerText = "AA";
                }
                // Select CCType node from all nodes
                var CCType = node.SelectSingleNode("//x:CCType", nsmgr);//<==
                if (CCType != null)
                {
                    // change value of CCType node to string
                    CCType.InnerText = "string";
                }



还可以使用root.OuterXml来显示VS对象的变化。这里:


Also use the "root.OuterXml" to show the changes in the object of VS. Here:

richTextBox1.Text = root.OuterXml;


这篇关于某些根信息被视为非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆