简体   繁体   English

使用C#进行XML解析?

[英]XML Parsing with C#?

I'm working on a project for school that involves a heavy amount of XML Parsing. 我正在研究一个涉及大量XML解析的学校项目。 I'm coding in C#, but I have yet to find a "suitable" method of parsing this XML out. 我在C#编码,但我还没有找到解析这个XML的“合适”方法。 There's several different ways I've looked at, but haven't gotten it right yet; 我看过几种不同的方式,但还没有把它弄好; so I have come to you. 所以我来找你 Ideally, I'm looking for something kind of similar to Beautiful Soup in Python (sort of). 理想情况下,我正在寻找类似于Python中的Beautiful Soup(有点类似)。

I was wondering if there was any way to convert XML like this: 我想知道是否有任何方法可以像这样转换XML:

<config>
    <bgimg>C:\\background.png</bgimg>
    <nodelist>
        <node>
            <oid>012345</oid>
            <image>C:\\image.png</image>
            <label>EHRV</label>
            <tooltip>
                <header>EHR Viewer</header>
                <body>Version 1.0</body>
                <icon>C:\\ico\ehrv.png</icon>
            </tooltip>
            <msgSource>8181:iqLog</msgSource>
        </nodes>
    </nodeList>
<config>

Into an Array/Hastable/Dictionary/Other like this: 进入一个数组/ Hastable / Dictionary / Other像这样:

Array
(
["config"] => array
    (
    ["bgimg"] => "C:\\background.png"
    ["nodelist"] => array
        (
        ["node"] => array
            (
            ["oid"] => "012345"
            ["image"] => "C:\\image.png"
            ["label"] => "Version 1.0"
            ["tooltip"] => array
                (
                ["header"] => "EHR Viewer"
                ["body"] => "Version 1.0"
                ["icon"] => "C:\\ico\ehrv.png"
                )
            ["msgSource"] => "8181:iqLog"
            )
        )
    )
)

Even just giving me a decent resource to look through would be really helpful. 即使只是给我一个体面的资源来查看也会非常有帮助。 Thanks a ton. 万分感谢。

I would look into Linq to Xml . 我会调查Linq到Xml This gives you an object structure similar to the Xml file that is fairly easy to traverse. 这为您提供了一个类似于Xml文件的对象结构,它非常容易遍历。

XmlDocument + XPath几乎就是你在.NET中解析XML所需的全部内容。

There must be 1/2 dozen different ways to do this in C#. 在C#中必须有二十几种不同的方法。 My favorite uses the System.Xml namespace, particularly System.Xml.Serialization . 我最喜欢的是使用System.Xml命名空间,特别是System.Xml.Serialization

You use a command line tool called xsd.exe to turn an xml sample into an xsd schema file (tip: make sure your nodelist has more than one node in the sample), and then use it again on the schema to turn that into a C# class file you can load into your project and easily use with the System.Xml.Serialization.XmlSerializer class. 您使用名为xsd.exe的命令行工具将xml示例转换为xsd架构文件(提示:确保您的节点列表在示例中有多个节点),然后在架构上再次使用它将其转换为您可以将C#类文件加载到项目中,并轻松使用System.Xml.Serialization.XmlSerializer类。

There's no shame in using an old-fashioned XmlDocument: 使用老式的XmlDocument并不羞耻:

var xml = "<config>hello world</config>";
var doc = new System.Xml.XmlDocument();
doc.LoadXml(xml);
var nodes = doc.SelectNodes("/config");

You should defiantly use LINQ to XML , AKA XLINQ. 您应该使用LINQ to XML ,AKA XLINQ。 There is a nice tool called LINQPad that you should check out. 有一个很棒的工具叫LINQPad ,你应该看看。 It has nice features, from a comprehensive examples library to allowing you to directly query an SQL database via Linq to SQL. 它具有很好的功能,从一个全面的示例库到允许您通过Linq直接查询SQL数据库到SQL。 Best of all, it lets you test your queries before putting them into code. 最重要的是,它允许您在将查询放入代码之前对其进行测试。

The best approach will be dictated by what you actually want to do with the data once you've parsed it out. 最好的方法将取决于您在解析数据后实际想要对数据执行的操作。

If you want to pass it around in a structured-but-not-tied-to-XML fashion, XML Serialization is probably your best bet. 如果您希望以结构化但未绑定到XML的方式传递它, XML序列化可能是您最好的选择。 This will also get you closest to what you've described, though you'll be dealing with an object graph rather than nested maps. 这也会让你最接近你所描述的,尽管你将处理一个对象图而不是嵌套的地图。

If you are just looking for a convenient format to query for specific bits of data, your best option would be LINQ to Xml . 如果您只是在寻找一种方便的格式来查询特定的数据位,那么最好的选择就是LINQ to Xml Alternatively, you could use the more traditional classes in the System.Xml namespace (starting with XmlDocument) and query using XPath. 或者,您可以使用System.Xml命名空间中更传统的类(从XmlDocument开始)和使用XPath进行查询。

You could also use any of these techniques (or an XmlTextReader ) as building blocks to create the datastructure you've described but, barring some special need, I don't think it'll give you any more versatility than what the other approaches will. 您也可以使用这些技术中的任何一种(或XmlTextReader )作为构建块来创建您所描述的数据结构,但是,除非有一些特殊需要,我认为它不会比其他方法更具有多功能性。 。

您还可以使用序列化将XML文本转换回强类型类实例。

I personally like to map XML elements to classes and viceversa using System.Xml.Serialization.XmlSerializer class. 我个人喜欢使用System.Xml.Serialization.XmlSerializer类将XML元素映射到类,反之亦然。

http://msdn.microsoft.com/es-es/library/system.xml.serialization.xmlserializer(VS.80).aspx http://msdn.microsoft.com/es-es/library/system.xml.serialization.xmlserializer(VS.80).aspx

I personally use XPathDocument, XPathNavigator and XPathNodeIterator eg 我个人使用XPathDocument,XPathNavigator和XPathNodeIterator等

XPathDocument xDoc = new XPathDocument(CHOOSE SOURCE!);

XPathNavigator xNav = xDoc.CreateNavigator();

XPathNodeIterator iterator = xNav.Select("nodes/node[@SomePredicate = 'SomeValue']");

while (iterator.MoveNext())
{
    string val = iterator.Current.SelectSingleNode("nodeWithValue");

    // etc etc
}

Yeah, i agree.. The linq-way is very nice. 是的,我同意.. linq-way非常好。 And i especially like the way you write XML using it. 我特别喜欢用它编写XML的方式。

It is much more simple using the "objects in objects"-way. 使用“物体中的物体”更加简单。

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

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