简体   繁体   English

替换InValid XML文件中的空格

[英]Replace Spaces in InValid XML File

My Question 我的问题

I have an xml file with invalid syntax first of all, second i cant do anything to change it, due to the company thats feeding the xml. 首先,我有一个语法无效的xml文件,其次,由于提供XML的公司,我无法做任何更改。

I have element nodes that i need to gut out the space, how can i do that VIA C# .NET 我有需要删除空间的元素节点,如何通过VIA C#.NET

I also have the option to get the data via a CSV format instead, thinking it might be easier to replace the values as a CSV rather then some XML String, but i just don't know how or the best way. 我还可以选择通过CSV格式获取数据,以为将值替换为CSV而不是一些XML字符串可能更容易,但我只是不知道如何或最好的方法。

Example

Provided: <Avg Pos> value </Avg Pos> 提供: <Avg Pos> value </Avg Pos>

Required: <AvgPos> value </AvgPos> 必需: <AvgPos> value </AvgPos>

You're probably going to be better off getting the CSV, because the XML they're feeding you is malformed. 获得CSV可能会更好,因为它们提供给您的XML格式错误。 In XML, because of the way tag naming works, there's a BIG difference between "TagName" and "Tag Name". 在XML中,由于标签命名的工作原理,“ TagName”和“ Tag Name”之间存在很大的区别。 It sounds like they're delivering completely invalid XML (NO XML client will be able to read that properly, according to the example you give); 听起来他们提供的是完全无效的XML(根据您提供的示例,没有XML客户端将能够正确读取该XML); you should try to get them to correct the error, but if they won't / can't, try dealing with the CSV. 您应该尝试让他们纠正错误,但如果不能/不能,请尝试处理CSV。

You better tell them what XML really means, 'cause that thing you posted there is far away from anything that has the right of being called XML... 您最好告诉他们XML的真正含义,因为您在此处发布的东西与任何有权被称为XML的东西相距甚远...

In that case, go for the CSV, read in every row and strip out the spaces (eg _Row.Replace(" ", ""); ), save it in a new .XML and you should be good to go. 在那种情况下,使用CSV,读取每一行并_Row.Replace(" ", "");空格(例如_Row.Replace(" ", ""); ),将其保存在新的.XML中,您应该会做得很好。

If you never have any attributes inside the tags you could: 如果您的代码中从未包含任何属性,则可以:

  1. figure out all the element names 找出所有元素名称
  2. determine their malformed open and close tag equivalent. 确定其格式错误的打开和关闭标签等效项。
  3. replace all malformed tags with their wellformed equivalent in the entire document 在整个文档中,将所有格式错误的标签替换为格式正确的标签

But if my employer gave me that stuff and said it was XML, I'd know it was time to switch jobs. 但是,如果我的老板给我这些东西并说它是XML,那我就该该换工作了。

This was really what i was looking for... 这真的是我想要的...

iReportPath iReportPath

TextReader origXML = new StreamReader(iReportPath + iFileName);

string validXMLString = HttpContext.Current.Server.HtmlEncode(origXML.ReadToEnd().ToString());

validXMLString = validXMLString.Replace("Avg Pos", "AvgPos");

validXMLString = validXMLString.Replace("Avg CPC", "AvgCPC");

validXMLString = validXMLString.Replace("Gr. Rev", "Gr.Rev");

validXMLString = HttpContext.Current.Server.HtmlDecode(validXMLString);

origXML.Close();

validXMLString value is what i needed! 我需要的是validXMLString值!

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

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