Xml:根元素上的“路径中的非法字符” [英] Xml: 'illegal characters in path' on root element

查看:80
本文介绍了Xml:根元素上的“路径中的非法字符”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我尝试读取XML文件的任何内容都落在根元素上?



<?xml version =1.0?>

< AnXMLTestFile xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns:xsd =http://www.w3.org/2001/XMLSchema xmlns =un:unece:260:data:EEM:02-02-AnXMLTestFile>



/ *使用XmlReader,XmlDocument,BOM,MemoryStreamer / Writer ,...

即使我省略了'编码GetEncoding()'方法,我仍然在路径'中有' 非法字符代码行

Why is anything that I try to read out the XML file is falling over the root element?

<?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">

/* Using XmlReader, XmlDocument, BOM, MemoryStreamer/Writer, ...
Even if I leave out the method 'Encoding GetEncoding()' I still have the 'illegal characters in path' on codeline

XmlTextReader reader = new XmlTextReader(doc.ToString());



* /

改进

清理了一些代码,只是为了使用基础知识。将字符串更改为使用


*/
Improvement:
cleaned up some code just to use the basics. changed the string to use

string xml = richTextBox1.Text;





现在代码没有遍历我的nodeList。



有人可以帮帮我吗?在此先感谢。



我尝试过:



XML-文件示例



Now the code doesn't loop over my nodeList.

Can somebody help me please? Thanks in advance.

What I have tried:

XML-file 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>
    <SenderBEEnergyParty>
      <Identification schemeAgencyIdentifier="9">5414488009809</Identification>
    </SenderBEEnergyParty>
    <RecipientBEEnergyParty>
      <Identification schemeAgencyIdentifier="9">0000000000000</Identification>  
    </RecipientBEEnergyParty>
    <IssuerBEEnergyParty>
      <Identification schemeAgencyIdentifier="9">5414488009809</Identification>
    </IssuerBEEnergyParty>
    <AddresseeBEEnergyParty>
      <Identification schemeAgencyIdentifier="9">0000000000000</Identification>
    </AddresseeBEEnergyParty>
    <DocumentStructureRevision>02-02.001</DocumentStructureRevision>
  </HeaderBEDocument>




        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            // Create an OpenFileDialog object.
            OpenFileDialog openFile1 = new OpenFileDialog();

            // Initialize the filter to look for text files.
            openFile1.Filter = "Xml Files|*.xml";

            // If the user selected a file, load its contents into the RichTextBox. 
            if (openFile1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
            }
        }
 private void btnMakeNegative_Click(object sender, EventArgs e)
        {
            // Load XML
            var doc = new XmlDocument();
            string xml = richTextBox1.Text;
            doc.LoadXml(xml);

            // Define Default namespace and add prefix to use in Xpath (Mandatory!)
            var nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("x", "un:unece:260:data:EEM:AnXMLTestFile");

            // Selecting All HeaderBEDocument nodes
            var nodeList = doc.DocumentElement.SelectNodes("//x:HeaderBEDocument", nsmgr);

            // Loop all HeaderBEDocument nodes
            foreach (XmlNode node in nodeList)
            {
                // Select Identification node under HeaderBEDocument node
                var identificationNode = node.SelectSingleNode("x:Identification", nsmgr);
                if (identificationNode != null)
                {
                    // Change value of indentification node to string
                    identificationNode.InnerText = "string";
                }
                // Select SCI node under HeaderBEDocument node
                var SCI = node.SelectSingleNode("x:SCI", nsmgr);
                if (SCI != null)
                {
                    // change value of SCI node to string
                    SCI.InnerText = "56987465";
                }
                // Select ReferenceType node under HeaderBEDocument node
                var ReferenceType = node.SelectSingleNode("x:ReferenceType", nsmgr);
                if (ReferenceType != null)
                {
                    // change value of ReferenceType node to string
                    ReferenceType.InnerText = "AA";
                }
                // Select CCType node under HeaderBEDocument node
                var CCType = node.SelectSingleNode("x:CCType", nsmgr);
                if (CCType != null)
                {
                    // change value of CCType node to string
                    CCType.InnerText = "string";
                }
            }

            // Print new XML in richtextbox
           richTextBox1.Text = nodeList.ToString();
        }

}

推荐答案

这与你的问题不相似 mscorlib.dll附加信息中出现未处理的'system.argumentexception'类型异常:非法字符路径。 [ ^ ]?



XmlTextReader构造函数(String)(System.Xml) [ ^ ]需要一个包含有效文件名的字符串,但是你要传递 doc.ToString()这可能不是文件名而是一些文件内容。
Isn't this similar to your question An unhandled exception of type 'system.argumentexception' occurred in mscorlib.dll additional information: illegal characters in path.[^]?

The XmlTextReader Constructor (String) (System.Xml)[^] expects a string containing a valid file name but you are passing doc.ToString() which is probably not a file name but some file content.


这不应该是是XML - 它是文件名和文件的路径,而不是XML文件内容。

它不是抱怨的XML,它是文件系统告诉你来自文件中的字符路径是不允许的 - 这意味着不是将文件名传递给您的方法,而是传递XML字符串。



使用调试器,检查值您传递的参数,然后尝试找出您将错误的东西传递给函数的原因。
That isn't supposed to be XML - it's a file name and the path to the file, not the XML file content.
It isn;t XML that's complaining, it's the file system telling you that come of the characters in your file path are not allowed - and that implies that instead of passing the file name to your method, you are passing an XML string.

Use the debugger, check the value of the parameter you pass and then try to work out why you have passed the wrong thing to the function.


1)传递NodeList循环,看看您添加的命名空间是否完整和文件中的相同。不要削减。

2)确保你没有使用现有的IEnumerables for XML。

-

- xmlns

- xml

像我一样添加一个指定的名字,命名为x。它将被添加到IEnumerable列表中。



代码完全不能正常工作但是这次我们已经通过了nodeList,这要归功于Jochen。

1) to pass the NodeList loop see through it that the namespace that you add is complete and the same as is in the file. Don't cut of.
2) Make sure you don't use one of the existing IEnumerables for XML.
- " "
- "xmlns"
- "xml"
add a specified one like I did, named "x". It will be added to the IEnumerable list.

Code is not working completely but we've passed the nodeList this time thanks to Jochen.
// Load XML
           var doc = new XmlDocument();
           string xml = richTextBox1.Text;
           doc.LoadXml(xml);

           XmlTextReader reader = new XmlTextReader(new StringReader(xml));
           System.Data.DataSet ds = new System.Data.DataSet();
           ds.ReadXml(reader, System.Data.XmlReadMode.Auto);

           // Define Default namespace and add prefix to use in Xpath (Mandatory!)
           var nsmgr = new XmlNamespaceManager(doc.NameTable);
           nsmgr.AddNamespace("x", "un:unece:260:data:EEM:02-02-AnXMLTestFile");

           // Selecting All HeaderBEDocument nodes
           var nodeList = doc.DocumentElement.SelectNodes("//x:HeaderBEDocument", nsmgr);

           // Loop all HeaderBEDocument nodes
           foreach (XmlNode node in nodeList)
           {
               // Select Identification node under HeaderBEDocument node
               var identificationNode = node.SelectSingleNode("x:Identification", nsmgr);
               if (identificationNode != null)
               {
                   // Change value of indentification node to string
                   identificationNode.InnerText = "string";
               }
               // Select SCI node under HeaderBEDocument node
               var SCI = node.SelectSingleNode("x:SCI", nsmgr);
               if (SCI != null)
               {
                   // change value of SCI node to string
                   SCI.InnerText = "56987465";
               }
               // Select ReferenceType node under HeaderBEDocument node
               var ReferenceType = node.SelectSingleNode("x:ReferenceType", nsmgr);
               if (ReferenceType != null)
               {
                   // change value of ReferenceType node to string
                   ReferenceType.InnerText = "AA";
               }
               // Select CCType node under HeaderBEDocument node
               var CCType = node.SelectSingleNode("x:CCType", nsmgr);
               if (CCType != null)
               {
                   // change value of CCType node to string
                   CCType.InnerText = "string";
               }
           }

           // Print new XML in richtextbox
          richTextBox1.Text = nodeList.ToString();
       }


这篇关于Xml:根元素上的“路径中的非法字符”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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