public void AddNodeToXml(string helpid, string fileName)
{
const string STR_EXPRESSION = "/Form/Controls/Control";
XPathDocument doc = null;
try
{
doc = new XPathDocument(fileName);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
if (doc != null)
{
XPathNavigator navigator = doc.CreateNavigator();
XPathNodeIterator localIterator = navigator.Select(STR_EXPRESSION);
while (localIterator.MoveNext())
{
if (localIterator.Current != null)
{
if (localIterator.Current.Name.Equals("Control"))
{
localIterator.Current.MoveToFirstAttribute();
if (localIterator.Current.Value.Equals(helpid))
{
localIterator.Current.MoveToParent();
localIterator.Current.CreateAttribute(string.Empty, "NewAttribute", string.Empty, "value");
}
}
}
}
}
}
내 XML 구조는 내가 currnet 리부팅 이름 속성 값이 "helpid"인 경우 제어 노드에 새로운 속성을 추가 할 STR_EXPRESSION 에 쇼로입니다 System.NotSupportedException을 던져, 나는 CreateAttribute()이 메소드를 사용하여 시도 그러나 System.NotSupportedException 예외를 제공합니다.CreateAttribute 방법이
당신은'사용하지 않는 이유 :
이 꽤 가까이 있어야 난 내 머리 위로 떨어져 쓴 테스트되지 않은 코드는하지만, 그것은 당신이 같은 문제를 해결하기 위해 Linq에를 사용하는 방법을 보여줍니다 XDocument' 및 LINQ to XML? – Oded
이유가 없으므로 이것이 더 쉬울 것이라고 생각했습니다. –