Parse XML document in C#

前端 未结 1 386
一个人的身影
一个人的身影 2020-12-01 16:17

Duplicate: This is a duplicate of Best practices to parse xml files with C#? and many others (see https://stackoverflow.com/search?q=c%23+parse+xml). Please

相关标签:
1条回答
  • 2020-12-01 16:44

    Try this:

    XmlDocument doc = new XmlDocument();
    doc.Load(@"C:\Path\To\Xml\File.xml");
    

    Or alternatively if you have the XML in a string use the LoadXml method.

    Once you have it loaded, you can use SelectNodes and SelectSingleNode to query specific values, for example:

    XmlNode node = doc.SelectSingleNode("//Company/Email/text()");
    // node.Value contains "test@ABC.com"
    

    Finally, note that your XML is invalid as it doesn't contain a single root node. It must be something like this:

    <Data>
        <Employee>
            <Name>Test</Name>
            <ID>123</ID>
        </Employee>
        <Company>
            <Name>ABC</Name>
            <Email>test@ABC.com</Email>
        </Company>
    </Data>
    
    0 讨论(0)
提交回复
热议问题