2

The following code takes an XmlNode data type and 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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Use XmlElement instead for get if the node is empty.

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

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.