开发者

Determine if a XmlNode is empty or null in C#?

开发者 https://www.devze.com 2023-02-06 12:03 出处:网络
The following code takes an XmlNode data type an开发者_如何学Cd populates a DataSet object with the XmlNode content.Then I write the dataset\'s content to file.

The following code takes an XmlNode data type an开发者_如何学Cd populates a DataSet object with the XmlNode content. Then I write the dataset's content to file.

public void PopulateDataSet(XmlNode node)
{
    XmlNodeReader reader = new XmlNodeReader(node);
    DataSet ds = new DataSet();
    ds.ReadXml(reader);

    system.Guid guid = System.Guid.NewGuid();
    string name = string.Format("{0}{1}_{2}.xml", Utility.XmlOutputPath, Utility.XmlOutputFileName, guid.ToString());

    //need to write "Node empty" to file if XmlNode object is empty of null
    ds.WriteXml(name, XmlWriteMode.IgnoreSchema);
}

The problem is that I encountered one scenario that it will not write the content to file. How do I determine if a XmlNode object is null or empty?


You can check if the node parameter is null or has InnerText or InnerXml properties are null or empty, immediately when you enter the method before even creating the XmlNodeReader.


Use XmlElement instead for get if the node is empty.

XmlElement currNode = (XmlElement) doc.DocumentElement.LastChild;
if (currNode.IsEmpty)
{
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消