3

I currently have a XML file format that goes something like this (whitespace and ellipses added for readability):

<root>
    <Module>          //Start with list of Modules
        <ModuleParams>
        </ModuleParams>
    </Module>
    ...

    <DetectLine>      //Now a list of DetectLines
        <DetectLineParams>
        </DetectLineParams>

        <Channels>    //List of Channels embedded in each DetectLine
            <Channel>
                <ChannelParams>
                </ChannelParams>
            </Channel>
            ...

        </Channels>
    </DetectLine>
    ...

</root>

Classes structured as follows:

public class Module
{
    public ModuleParams { get; set; }
}

public class DetectLine
{
    public DetectLineParams { get; set; }
    public List<Channel> Channels { get; set; }
}

public class Channel
{
    public ChannelParams { get; set; }
}

The list of Modules and DetectLines are easy to parse with Linq to XML. However, parsing the Channel list for each DetectLine is not as apparent to me. Can this even be done with Linq to XML? I would prefer not having to restructure things to work with a XMLSerializer.

Initial Code (openXML is a OpenFileDialog. Already checked for good filename):

List<Module> myModules;
List<DetectLine> myDetectLines;
XDocument config = XDocument.Load(openXML.FileName);

myModules =
         (from myElements in config.Descendants("Module")
          select new Module()
          {
              ModuleParam1 = (string)myElements.Element("ModuleParam1"),
              ModuleParam2 = (string)myElements.Element("ModuleParam2"),
              ...
          }).ToList<Module>();

myDetectLines =
          (from myElements in config.Descendants("DetectLine")
           select new DetectLine()
           {
              DetectLineParam1 = (string)myElements.Element("ModuleParam1"),
              DetectLineParam2 = (string)myElements.Element("ModuleParam2"),
              ...

              // ?? Add Channels list here somehow...
           }).ToList<DetectLine>();
4

1 回答 1

2

XElement detectLine = XElement.Parse(@"<DetectLine>      
        <DetectLineParams>
        </DetectLineParams>

        <Channels>    
            <Channel>
                <ChannelParams>
                </ChannelParams>
            </Channel>
            ...

        </Channels>
    </DetectLine>
");

你可以做例如

DetectLine dl1 = new DetectLine() {
  DetectLineParams = ...,
  Channels = (from channel in detectLine.Element("Channels").Element("Channel")
             select new Channel() {
                 ChannelParams = new ChannelParams() { ... = channel.Element("ChannelParams").Value }
             }).ToList();

我们确实需要查看更多具体的 C# 类代码来说明如何设置完整的查询。

[编辑] 为了适应您现在发布的代码:

myDetectLines =
          (from myElements in config.Descendants("DetectLine")
           select new DetectLine()
           {
              DetectLineParam1 = (string)myElements.Element("ModuleParam1"),
              DetectLineParam2 = (string)myElements.Element("ModuleParam2"),
              ...

              Channels = (from channel in myElements.Element("Channels").Element("Channel")
                 select new Channel() {
                     ChannelParams = new ChannelParams() { ... = channel.Element("ChannelParams").Value }
                 }).ToList();
           }).ToList<DetectLine>();
于 2012-06-04T16:00:48.240 回答