开发者

how to check if xml document has a certain list of attributes?

i am experimenting with C# and xml, i am trying to read a XML file i want to validate if "NumberOfDays" , "NumberOfBooks",开发者_如何学编程 "NumberOfExam", "CurrentDate" are existing, if missing. i want to add them with there values.

i have the following xmldocument :

<?xml version="1.0" encoding="utf-8" ?>
<MySample>
  <Content>
    <add  key="NumberOfDays" value="31"  />
    <add  key="NumberOfBooks" value="20"  />
    <add  key="NumberOfExam" value="6"  />
    <add  key="CurrentDate" value="15 - Jul - 2011"  />
  </Content>
</MySample>

i am writing a sample application in c# using

--------Edit--------

thank you AresAvatar for your responce.

but if the value exists i would like to update its value i.e. if let's say

<add  key="NumberOfExam" value=""  />

i want to change the value to 6


You can get a list of which nodes exist like this:

// Get a list of which nodes exist
XmlDocument doc = new XmlDocument();
doc.LoadXml(myXml);
List<string> existingNodes = new List<string>();
XmlNodeList bookNodes = doc.SelectNodes("/MySample/Content/add");
foreach (XmlNode nextNode in bookNodes)
{
    foreach (XmlAttribute nextAttr in nextNode.Attributes)
        existingNodes.Add(nextAttr.InnerText);
}

You can add missing nodes like this:

// Add nodes
XmlNode addRoot = doc.SelectSingleNode("/MySample/Content");
XmlElement addme = doc.CreateElement("add");
addme.SetAttribute("NumberOfDays", "31");
addRoot.AppendChild(addme);

You can set the value of existing nodes like this:

// Update a node
foreach (XmlNode nextNode in bookNodes)
{
    foreach (XmlAttribute nextAttr in nextNode.Attributes)
    {
        switch (nextAttr.Name)
        {
            case "NumberOfDays":
                ((XmlElement)nextNode).SetAttribute("value", "31");
                break;
            // etc.
        }
    }
}


First of all, if you have control over the generated XML (if you make it yourself), avoid using this schema:

<?xml version="1.0" encoding="utf-8" ?>
<MySample>
  <Content>
    <add  key="NumberOfDays" value="31"  />
    <add  key="NumberOfBooks" value="20"  />
    <add  key="NumberOfExam" value="6"  />
    <add  key="CurrentDate" value="15 - Jul - 2011"  />
  </Content>
</MySample>

It's much easier to use with that schema:

<?xml version="1.0" encoding="utf-8" ?>
    <MySample>
      <Content>
       <Adds>
        <NumberOfDays>31<NumberOfDays/>
        <NumberOfBooks>20<NumberOfBooks/>
        <NumberOfExam>6<NumberOfExam/>
        <CurrentDate>5 - Jul - 2011<CurrentDate/>
       </Adds>
      </Content>
    </MySample>

And then:

XmlDocument doc = new XmlDocument();
doc.Load("YourXmlPath");
XmlNode firstNode = doc["MySample"];
if(firstNode != null)
{
    XmlNode secondNode = firstNode["Content"];
    if(secondNode != null)
    {
        XmlNode thirdNode = secondNode["Adds"];
        if(thirdNode != null)
        {
            if(thirdNode["NumberOfDays"] == null) //The "NumberOfDays" node does not exist, we create it.
            {
                XmlElement newElement = doc.CreateElement("NumberOfDays");
                newElement.InnerXml = 31;
                thirdNode.AppendChild(newElement);                    
            }
            //Do the same for the other nodes...
        }
    }
}
doc.Save("YourXmlPath");

Just remember to check for null on every node, or put the whole block into a try/catch.

And to save the XML once you did your changes.

XmlDocument.Load() function loads the XML in memory, so you can check for any node without making "blind loops".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜