c# xml multiple elements
These are my 2 attempts at retrieving these multiple elements, the 1st only selects the first box & first text element
the 2nd retrieves all, but they are not separate variables--
var xmlDoc = document.Root.Elements("Page")
                          .Select(element => new
                          {
                             Box = (string)element.Element("Box"),
                             Text = (string)element.Element("Text"),
                          }).ToList();
var xmlDoc = document.Root.Descendants("Page")
                          .Elements()
                          .Select(x => x.Value)
                          .ToList();
xml sample:
<?xml version="1.0" standalone="yes"?>
<PrintJob>
<Page>
<Box>0000,0000,0190,0135</Box>
<Box>0050,0100,0190,0135</Box>
<Text>0000,0000,"ABCdef123456"</Text>
<Text>0000,0000,"ABCdef123456"</Text>
<Text>0000,0050,"ABCdef123456"</Text>
<Text>0000,0050,"ABCdef123456"</Text>
</Page>
<开发者_JS百科;/PrintJob>
loop to retrieve elements:
foreach (var x in xmlDoc)
{
   //result += "box: " + x.Box + "\n";
   //result += "text: " + x.Text + "\n";
   result += "x: " + x + "\n";
}
how to I get results like this:
box: 0000,0000,0190,0135
box: 0050,0100,0190,0135
text: 0000,0000,"ABCdef123456"
text: 0000,0000,"ABCdef123456"
text: 0000,0050,"ABCdef123456"
text: 0000,0050,"ABCdef123456"
Are you looking for something like this?
var page = document.Element("PrintJob")
                   .Element("Page");
var boxes = page.Elements("Box")
                .Select(x => (string)x)
                .ToList();
var texts = page.Elements("Text")
                .Select(x => (string)x)
                .ToList();
foreach (var box in boxes)
    Console.WriteLine("Box: " + box);
foreach (var text in texts)
    Console.WriteLine("Text: " + text);
Output:
Box: 0000,0000,0190,0135 Box: 0050,0100,0190,0135 Text: 0000,0000,"ABCdef123456" Text: 0000,0000,"ABCdef123456" Text: 0000,0050,"ABCdef123456" Text: 0000,0050,"ABCdef123456"
var items = document.Element("PrintJob")
                    .Element("Page")
                    .Elements()
                    .Select(x => x.Name.LocalName + ": " + (string)x)
                    .ToList();
foreach (var item in items)
    Console.WriteLine(item);
Output:
Box: 0000,0000,0190,0135 Box: 0050,0100,0190,0135 Text: 0000,0000,"ABCdef123456" Text: 0000,0000,"ABCdef123456" Text: 0000,0050,"ABCdef123456" Text: 0000,0050,"ABCdef123456"
var items = document.Element("PrintJob")
                    .Element("Page")
                    .Elements()
                    .Select(x => new
                                 {
                                     Box  = (x.Name.LocalName == "Box")
                                          ? (string)x
                                          : null,
                                     Text = (x.Name.LocalName == "Text")
                                          ? (string)x
                                          : null
                                 })
                    .ToList();
foreach (var item in items)
    Console.WriteLine("Box: " + item.Box + " Text: " + item.Text);
Output:
Box: 0000,0000,0190,0135 Text: Box: 0050,0100,0190,0135 Text: Box: Text: 0000,0000,"ABCdef123456" Box: Text: 0000,0000,"ABCdef123456" Box: Text: 0000,0050,"ABCdef123456" Box: Text: 0000,0050,"ABCdef123456"
var xmlDoc = document.Root.Descendants("Page")
                          .Elements();
foreach(var x in xmlDoc)
{
    Console.WriteLine(x.Name + ": " + x.Value);
}
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论