简体   繁体   中英

Verify node exists before getting its value

I Stored the xml values in one string using C#.

string abcd="<xstructure><a>
   <a1>1</a1>
   <a2>2</a2>
   <a3>3</a3>
</a>

<b>4</b>
</xstructure>";

I retrieved the xml values from the string like,

 var xElem = XElement.Parse(abcd);
 string b= xElem.Element("b").Value;

it was working fine.How to Check the Xml Node Exist in the XML Structure or Not? if i try to get the C value from the structure the C value does not exist in the XML structure.So i need to check the c value available or not,before i try to get the C value .How can I do this?

Try,

XElement c = xElem.Element("c");
if(null != c)
{
   // do something with c because it exists, like...
   string cValue = c.Value;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM