0

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>
</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"
0

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

5 Comments

looks good, can it be done with just one list, like a variation of my 1st example? -- maybe that's not the way to go, but trying to understand this--
from your 2nd answer, how can you do: foreach(var item in items) Console.WriteLine("Box: " + item.Box + "Text: " + item.Text )
@Scott: What should be the output for this?
the output will look the same, just need to pick out the separate box/text elements ultimately, and I like the 2nd example--
@Scott: Added another example. You should probably use custom classes instead of an anonymous type, for instance, abstract class PageItem { } class Box : PageItem { } class Text : PageItem { }.
0
var xmlDoc = document.Root.Descendants("Page")
                          .Elements();
foreach(var x in xmlDoc)
{
    Console.WriteLine(x.Name + ": " + x.Value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.