[英]Replace all string of existing xml file?
I'm trying to replace the existing .xml extension file's inside text with String. 我正在尝试将现有的.xml扩展文件的内部文本替换为String。
Why I'm doing this is: I wanted to read images from .xml file. 我这样做的原因是:我想从.xml文件读取图像。 But image did not loading file from the existing folder of .xml file.
但是图像没有从.xml文件的现有文件夹中加载文件。 I tried so many times to solve this problem.
我尝试了很多次来解决这个问题。 And only possible thing was the put the hard path of the Image file destination.
唯一可能的是放置Image文件目标的硬路径。 So then I started to try this:
因此,我开始尝试以下方法:
<img src='
" existed area from string - Solved <img src='
”存在的区域-已解决 Purpose of the work is "Show images from XML file". 工作的目的是“显示XML文件中的图像”。 So please someone answer unsolved article.
因此,请有人回答未解决的文章。 And don't suggest me about methods like Base64ToImage.
也不要建议我使用Base64ToImage之类的方法。 I just want to go like this way.
我只想这样走。 Hope I will have my answer here.
希望我能在这里得到答案。
existing XML: <helpRoot> <body Type="Section1"> <![CDATA[ <img src='Help1.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section1</h3></hr>
<p style="color:#484848;font-family:open sans;">Text is shown</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is not showing</h3></hr>
]]></body> <body Type="Section2"> <![CDATA[ <img src='Help2.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section2</h3></hr>
<p style="color:#484848;font-family:open sans;">Text is shown</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is not showing</h3></hr>
]]></body> ... </helpRoot>现有XML:
<helpRoot> <body Type="Section1"> <![CDATA[ <img src='Help1.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section1</h3></hr>
<p style="color:#484848;font-family:open sans;">Text is shown</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is not showing</h3></hr>
]]></body> <body Type="Section2"> <![CDATA[ <img src='Help2.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section2</h3></hr>
<p style="color:#484848;font-family:open sans;">Text is shown</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is not showing</h3></hr>
]]></body> ... </helpRoot><helpRoot> <body Type="Section1"> <![CDATA[ <img src='Help1.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section1</h3></hr>
<p style="color:#484848;font-family:open sans;">Text is shown</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is not showing</h3></hr>
]]></body> <body Type="Section2"> <![CDATA[ <img src='Help2.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section2</h3></hr>
<p style="color:#484848;font-family:open sans;">Text is shown</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is not showing</h3></hr>
]]></body> ... </helpRoot>
new XML string: <helpRoot> <body Type="Section1"> <![CDATA[ <img src='D:/Project/Image/Help1.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section1</h3></hr>
<p style="color:#484848;font-family:open sans;">Text has showing</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is showing</h3></hr>
]]></body> <body Type="Section2"> <![CDATA[ <img src='D:/Project/Image/Help2.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section2</h3></hr>
<p style="color:#484848;font-family:open sans;">Text has shown</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is not showing</h3></hr>
]]></body> ... </helpRoot>新的XML字符串:
<helpRoot> <body Type="Section1"> <![CDATA[ <img src='D:/Project/Image/Help1.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section1</h3></hr>
<p style="color:#484848;font-family:open sans;">Text has showing</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is showing</h3></hr>
]]></body> <body Type="Section2"> <![CDATA[ <img src='D:/Project/Image/Help2.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section2</h3></hr>
<p style="color:#484848;font-family:open sans;">Text has shown</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is not showing</h3></hr>
]]></body> ... </helpRoot><helpRoot> <body Type="Section1"> <![CDATA[ <img src='D:/Project/Image/Help1.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section1</h3></hr>
<p style="color:#484848;font-family:open sans;">Text has showing</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is showing</h3></hr>
]]></body> <body Type="Section2"> <![CDATA[ <img src='D:/Project/Image/Help2.png'/>
<h3 style="color:#2B94EA;font-family:open sans;">Section2</h3></hr>
<p style="color:#484848;font-family:open sans;">Text has shown</p> <h3 style="color:#3399FF;font-family:open sans;"> Image is not showing</h3></hr>
]]></body> ... </helpRoot>
I'd use XDocument
and HTMLAgilityPack
我会使用
XDocument
和HTMLAgilityPack
var xml =
@"<base>
<child><![CDATA[<img src=""/path"" />]]></child>
</base>";
var xDocument = XDocument.Parse(xml);
var xChild = xDocument.Descendants("child").First();
var cData = xChild.Nodes().OfType<XCData>().First();
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(cData.Value);
var imgNode = htmlDocument.DocumentNode.SelectSingleNode("img");
imgNode.Attributes["src"].Value = "/new/path";
using (var writer = new StringWriter())
{
htmlDocument.Save(writer);
cData.Value = writer.ToString();
}
xDocument
now contains: xDocument
现在包含:
<base>
<child><![CDATA[<img src="/new/path">]]></child>
</base>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.