在c#中解析字符串
假设有一个如下所示的 xml 文件:
<Instances>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image1.jpg" ImageNumber = "1"/>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image2.jpg" ImageNumber = "2"/>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image3.jpg" ImageNumber = "3"/>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image4.jpg" ImageNumber = "4"/>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image5.jpg" ImageNumber = "5"/>
</Instances>
该 xml 文件作为字符串读取并传递给函数。此 xml 文件包含有关特定图像文件的信息。我想从此字符串中提取所有图像文件的位置。因此,无论提交的“位置”的价值是什么,我都需要收集所有这些价值。在 C# 中实现此目的的最佳方法是什么?
谢谢,
Suppose there is an xml file like below:
<Instances>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image1.jpg" ImageNumber = "1"/>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image2.jpg" ImageNumber = "2"/>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image3.jpg" ImageNumber = "3"/>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image4.jpg" ImageNumber = "4"/>
<Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image5.jpg" ImageNumber = "5"/>
</Instances>
This xml file is read as a string and passed on to a function. This xml file has information about a particular image file. I want to extract the location of all the image files from this string. So whatever is value of "location" filed i need to collect all those value. What is the best way to achieve this in C#.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最简单的方法:将其解析为 XML(我建议使用 LINQ to XML),然后使用 XML API 附加信息。自己将其视为原始角色数据是没有意义的。
示例:(
这将为任何不包含
Location
属性的Bits
元素提供 null。)Simplest way: parse it as XML (I'd suggest using LINQ to XML) and then extra the information using the XML API. There's no point in treating it as raw character data yourself.
Sample:
(That will give a null for any
Bits
element which didn't contain aLocation
attribute.)请注意,此处的结构不是 XElement.Parse 的有效 XML,因为您的元素没有名称,只有属性。
可能的正确结构是:
这些将导致以下用于解析的 C# 代码 - 基于上面 Jon Skeet 的代码:
HTH :)
Take care, your structure here is not a valid XML for the XElement.Parse because your elements do not have a name, but only attributes.
A possible correct structure would be:
These would result in folowing C# Code for Parsing - based on Jon Skeet's code from above:
HTH :)
不使用字符串。如果它是 XML,则按原样读取它并使用 XML LINQ 库查询它。
Not using a string. If it's XML, then read it as such and query it using the XML LINQ libraries.
如果您要解析 XML,请使用框架中的 XML 类,特别是 XElement。
使用加载数据
然后您可以使用定义良好的 API 轻松操作对象。
If you are parsing XML use the XML classes in the framework, particularly XElement.
Load your data with
Then you can easily manipulate the objects with a well defined API.
我建议使用 Linq to XML。通过简单的 Linq 查询,您可以获得位置;不需要解析。
I would suggest using Linq to XML. With a simple Linq query you could obtain the Location; not parsing necessary.
您可以为此使用 Xpath 表达式
you may use Xpath expression for this