0

I have an xml file like:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Session TimeStamp="2016-12-21T17:01:01.8642453+02:00">
    <Message>
      <Content>test1</Content>
      <ID>1</ID>
      <Timestamp>12/21/2016 17:01:01</Timestamp>
      <EventType>Debug</EventType>
      <Priority>High</Priority>
    </Message>
    <Message>
      <Content>test2</Content>
      <ID>2</ID>
      <Timestamp>12/21/2016 17:01:01</Timestamp>
      <EventType>Exception</EventType>
      <Priority>Low</Priority>
    </Message>
    <Message>
      <Content>test3</Content>
      <ID>3</ID>
      <Timestamp>12/21/2016 17:01:01</Timestamp>
      <EventType>Info</EventType>
      <Priority>Medium</Priority>
    </Message>
    <Message>
      <Content>test4</Content>
      <ID>4</ID>
      <Timestamp>12/21/2016 17:01:01</Timestamp>
      <EventType>Warn</EventType>
      <Priority>None</Priority>
    </Message>
  </Session>
</Root>

I want to check the value of element Content in every message i have try with this method:

Assert.IsTrue(xDocument.Root.Elements("Session").Last().Elements("Message").First().Element("Content").Value.Contains("test1"));

exception: System.InvalidOperationException: Sequence contains no elements

The method fail, cant find the element value, how can i do it using xdocument?

2
  • I suggest you split this long statement into shorter ones and step through them in the debugger. This way you can see which statement fails. And hopefully looking at the variables will give you some hints on why it fails. Commented Dec 22, 2016 at 8:08
  • Your code works when I try to replicate it. How are you parsing the text to the xDocument? Are you using XDocument.Parse? Commented Dec 22, 2016 at 8:16

3 Answers 3

1

Are you looking for this since you say

I want to check the value of element Content in every message

xDocument.Root.Elements("Session")
              .Elements("Message")
              .Elements("Content")
              .Select(x => x.Value.Contains("test1"));

It would return which node contains test1 so the result would be true,false,false,false

Edit

as per your comment "i want only to verify if message 1 content contains string "test1" "

    xDocument.Root.Elements("Session")
              .Elements("Message")
              .Elements("Content")
              .FirstOrDefault().Value.Contains("test1");
Sign up to request clarification or add additional context in comments.

1 Comment

i want only to verify if message 1 content contains string "test1"
0
XmlDocument advDoc=new XmlDocument();
advDoc.Load("test.xml");
XmlNodeList _ngroups = advDoc.GetElementsByTagName("Content");
foreach(XmlNode nd in _ngroups)
{
  if(nd.InnerText.ToString()=="test1")
   Console.WriteLine("true");
}

1 Comment

Code-only answers like this are not very clear. Please explain what your code does.
0

i want only to verify if message 1 content contains string "test1"

string pathToXmlFile = ""; // point to your xml file ... 
using (StreamReader reader = File.OpenText(pathToXmlFile))
{
    XDocument doc = XDocument.Load(reader); // load into XDocument
    XElement idElement = doc.Root.Element("Session").Elements("Message").Elements("ID").First( item => item.Value == "1"); // since you need the message id = 1
    string content = idElement.Parent.Element("Content").Value; // get the parent of this message id which is message element then navigate to its content element.
}

Hope this helps..

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.