问题描述
为什么我尝试读取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();
}
}
推荐答案
// 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:根元素上的“路径中的非法字符”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!