尝试读取文件时遇到问题。
XML文件的内容包含无效字符。该字符为'&'
。
我尝试通过替换无效字符和try / catch来解决问题,但我还有其他异常,因为该文件正由另一个程序使用。
以下是我的XML示例:
<?xml version="1.0" encoding="ISO-8859-1"?>
<ALABAMAOGANYWHERE file="" LSP="73089617000107" datetime="20170201|094839" records="1">
<NEWSHIPMENT id="701781" type="OUT&BOUND" dtRequest="20170201|000000">
<ORIGIN name="Sousas & Sousas LTDA" address="RUA PAUL GROUND , 1415" country="BRASIL" state="PR" city="CURITIBA" cdPostal="81460040"/>
</NEWSHIPMENT>
</ALABAMAOGANYWHERE>
这是我尝试阅读的方式:
public void lerArquivoXML()
{
string[] arquivos = Directory.GetFiles(@"C:\Users\la\Documents\teste");
foreach (string arq in arquivos)
{
XmlDocument doc = new XmlDocument();
try
{
doc.Load(arq);
}
catch (System.Xml.XmlException)
{
//replace---------------------------------------------------------------------------
Replace replace = new Replace();
replace.ReplaceTextInFile(arq, arq, "&", "");
doc.Load(arq);
//end_replace------------------------------------------------------------------------
}
String ORIGIN_Name, ORIGIN_Address, ORIGIN_Country, ORIGIN_State, ORIGIN_City, ORIGIN_cdPostal, ORIGIN_cnpj, ORIGIN_IE, DESTINY_Name, DESTINY_Address, DESTINY_Country, DESTINY_State, DESTINY_City, DESTINY_CD_Postal, DESTINY_CNPJ, DESTINY_IE, DESTINY_PHONE, DESTINY_PHONE2, MODAL, DELLINSTRUCTION, SALESREPMAIL, SALESREPCODIGO, NF_VALUE, NF_DAISSUE, NFServico, PO, DESCRIPTION_VALUE, PRODUCT_PAYMENTCC, PRODUCT_LOB, PRODUCT_TYPE, PRODUCT_qtTotal, PRODUCT_wgTotal, PRODUCT_vlTotal, DELIVERYPLUS_dpservicename, DELIVERYPLUS_dpcomplementaryinfo, NFSerie, SEGMENT, SA3, EDD, SHIFT_VALUE, Customer_Number;
ORIGIN_Name = ORIGIN_Address = ORIGIN_Country = ORIGIN_State = ORIGIN_City = ORIGIN_cdPostal = ORIGIN_cnpj = ORIGIN_IE = DESTINY_Name = DESTINY_Address = DESTINY_Country = DESTINY_State = DESTINY_City = DESTINY_CD_Postal = DESTINY_CNPJ = DESTINY_IE = DESTINY_PHONE = DESTINY_PHONE2 = MODAL = DELLINSTRUCTION = SALESREPMAIL = SALESREPCODIGO = NF_VALUE = NF_DAISSUE = NFServico = PO = DESCRIPTION_VALUE = PRODUCT_PAYMENTCC = PRODUCT_LOB = PRODUCT_TYPE = PRODUCT_qtTotal = PRODUCT_wgTotal = PRODUCT_vlTotal = DELIVERYPLUS_dpservicename = DELIVERYPLUS_dpcomplementaryinfo = NFSerie = SEGMENT = SA3 = EDD = SHIFT_VALUE = Customer_Number = "";
try
{
XmlNodeList node = doc.GetElementsByTagName("NEWSHIPMENT");
foreach (XmlElement nodo in node)
{
ORIGIN_Name = nodo.GetElementsByTagName("ORIGIN")[0].Attributes["name"].Value;
ORIGIN_Address = nodo.GetElementsByTagName("ORIGIN")[0].Attributes["address"].Value;
ORIGIN_Country = nodo.GetElementsByTagName("ORIGIN")[0].Attributes["country"].Value;
ORIGIN_State = nodo.GetElementsByTagName("ORIGIN")[0].Attributes["state"].Value;
ORIGIN_City = nodo.GetElementsByTagName("ORIGIN")[0].Attributes["city"].Value;
ORIGIN_cdPostal = nodo.GetElementsByTagName("ORIGIN")[0].Attributes["cdPostal"].Value;
ORIGIN_cnpj = nodo.GetElementsByTagName("ORIGIN")[0].Attributes["cnpj"].Value;
ORIGIN_IE = nodo.GetElementsByTagName("ORIGIN")[0].Attributes["ie"].Value;
DESTINY_Name = nodo.GetElementsByTagName("DESTINY")[0].Attributes["name"].Value;
DESTINY_Address = nodo.GetElementsByTagName("DESTINY")[0].Attributes["address"].Value;
DESTINY_Country = nodo.GetElementsByTagName("DESTINY")[0].Attributes["country"].Value;
DESTINY_State = nodo.GetElementsByTagName("DESTINY")[0].Attributes["state"].Value;
DESTINY_City = nodo.GetElementsByTagName("DESTINY")[0].Attributes["city"].Value;
DESTINY_CD_Postal = nodo.GetElementsByTagName("DESTINY")[0].Attributes["cdPostal"].Value;
DESTINY_CNPJ = nodo.GetElementsByTagName("DESTINY")[0].Attributes["cnpj"].Value;
DESTINY_IE = nodo.GetElementsByTagName("DESTINY")[0].Attributes["ie"].Value;
DESTINY_PHONE = nodo.GetElementsByTagName("DESTINY")[0].Attributes["phone"].Value;
DESTINY_PHONE2 = nodo.GetElementsByTagName("DESTINY")[0].Attributes["phone2"].Value;
DESTINY_PHONE = nodo.GetElementsByTagName("DESTINY")[0].Attributes["phone"].Value;
DESTINY_PHONE2 = nodo.GetElementsByTagName("DESTINY")[0].Attributes["phone2"].Value;
MODAL = nodo.GetElementsByTagName("MODAL")[0].Attributes["value"].Value;
DELLINSTRUCTION = nodo.GetElementsByTagName("DELLINSTRUCTION")[0].Attributes["value"].Value;
SALESREPMAIL = nodo.GetElementsByTagName("SALESREPMAIL")[0].Attributes["value"].Value;
SALESREPCODIGO = nodo.GetElementsByTagName("SALESREPCODIGO")[0].Attributes["value"].Value;
XmlNodeList node_product = (nodo).GetElementsByTagName("PRODUCTS");
foreach (XmlElement nodo_product in node_product)
{
NF_VALUE = nodo_product.GetElementsByTagName("NF")[0].Attributes["value"].Value;
NF_DAISSUE = nodo_product.GetElementsByTagName("NF")[0].Attributes["daIssue"].Value;
PO = nodo_product.GetElementsByTagName("PO")[0].Attributes["value"].Value;
DESCRIPTION_VALUE = nodo_product.GetElementsByTagName("DESCRIPTION")[0].Attributes["value"].Value;
PRODUCT_PAYMENTCC = nodo_product.GetElementsByTagName("PRODUCT")[0].Attributes["paymentcc"].Value;
PRODUCT_LOB = nodo_product.GetElementsByTagName("PRODUCT")[0].Attributes["lob"].Value;
PRODUCT_TYPE = nodo_product.GetElementsByTagName("PRODUCT")[0].Attributes["type"].Value;
PRODUCT_qtTotal = nodo_product.GetElementsByTagName("PRODUCT")[0].Attributes["qtTotal"].Value;
PRODUCT_wgTotal = nodo_product.GetElementsByTagName("PRODUCT")[0].Attributes["wgTotal"].Value;
PRODUCT_vlTotal = nodo_product.GetElementsByTagName("PRODUCT")[0].Attributes["vlTotal"].Value;
DELIVERYPLUS_dpservicename = nodo_product.GetElementsByTagName("DELIVERYPLUS")[0].Attributes["dpservicename"].Value;
DELIVERYPLUS_dpcomplementaryinfo = nodo_product.GetElementsByTagName("DELIVERYPLUS")[0].Attributes["dpcomplementaryinfo"].Value;
NFSerie = nodo_product.GetElementsByTagName("NFSerie")[0].Attributes["value"].Value;
SEGMENT = nodo_product.GetElementsByTagName("SEGMENT")[0].Attributes["value"].Value;
SA3 = nodo_product.GetElementsByTagName("SA3")[0].Attributes["value"].Value;
EDD = nodo_product.GetElementsByTagName("EDD")[0].Attributes["value"].Value;
SHIFT_VALUE = nodo_product.GetElementsByTagName("SHIFT")[0].Attributes["value"].Value;
}
}
}
catch (ArgumentOutOfRangeException) { }
counterXML++;
try
{
dadosXML.GravarXML(ORIGIN_Name, ORIGIN_Address, ORIGIN_Country, ORIGIN_State, ORIGIN_City, ORIGIN_cdPostal, ORIGIN_cnpj, ORIGIN_IE, DESTINY_Name, DESTINY_Address, DESTINY_Country, DESTINY_State, DESTINY_City,
DESTINY_CD_Postal, DESTINY_CNPJ, DESTINY_IE, DESTINY_PHONE, DESTINY_PHONE2, MODAL,
DELLINSTRUCTION, SALESREPMAIL, SALESREPCODIGO, NF_VALUE, NF_DAISSUE, NFServico, PO,
DESCRIPTION_VALUE, PRODUCT_PAYMENTCC, PRODUCT_LOB, PRODUCT_TYPE, PRODUCT_qtTotal, PRODUCT_wgTotal,
PRODUCT_vlTotal, DELIVERYPLUS_dpservicename, DELIVERYPLUS_dpcomplementaryinfo, NFSerie, SEGMENT, SA3, EDD, SHIFT_VALUE, Customer_Number);
}
catch (Exception ex)
{
}
System.Console.WriteLine("There were {0} lines,", counterXML);
}
}
类替换
class Replace
{
#region
public void ReplaceTextInFile(string originalFile, string outputFile, string searchTerm, string replaceTerm)
{
string tempLineValue;
using (FileStream inputStream = File.OpenRead(originalFile))
{
using (StreamReader inputReader = new StreamReader(inputStream))
{
using (StreamWriter outputWriter = File.AppendText(outputFile))
{
while (null != (tempLineValue = inputReader.ReadLine()))
{
outputWriter.WriteLine(tempLineValue.Replace(searchTerm, replaceTerm));
}
outputWriter.Close();
}
}
}
}
#endregion
}
答案 0 :(得分:1)
试试
doc.LoadXml(File.ReadAllText(arq).Replace("&", "&"));
而不是
doc.Load(arq);
在你的try块中