2012-02-08 4 views
0

config 파일을 업데이트하는 프로그램이 있습니다. 예를 들어, 설정 파일이 포함될 수 있습니다하나의 노드가 null 인 경우 단일 노드 생성

<configuration> 
    <userSettings> 
    <setting name="phoneNumber" serializeAs="String"> 
     <value>123-456-7890</value> 
    </setting> 
    </userSettings> 
</configuration> 

이 설정 파일을 업데이트하려면, 내가 사용하는 다음

XmlNode phoneNumberNode = theConfig.SelectSingleNode("configuration/userSettings/setting[@name='phoneNumber']"); 
phoneNumberNode.FirstChild.InnerText = this._cloudPublisherWebURL; 

지금, 업데이트하는 동안 나는 PHONENUMBER 및 주소를 업데이트합니다. 주소 은 설정 파일에서 일 수도 있고 아닐 수도 있습니다.

SelectSingleNode가 null 인 경우 주어진 경로를 사용하여 노드를 만들고 그 값을 설정하려고합니다.

XmlNode addressNode = theConfig.SelectSingleNode("configuration/userSettings/setting[@name='address']"); 
if(addressNode == null) 
{ 
    //..Create the node here 
} 

주어진 경로에 값이있는 노드를 만들려면 어떻게해야합니까?

+0

이 항목이 중복 되었습니까? http://stackoverflow.com/questions/508390/create-xml-nodes-based-on-xpath/509340#509340 – xcud

답변

0
XmlNode addressNode = theConfig.SelectSingleNode("configuration/userSettings"); 
XmlNode setting = addressNode.Item(0).SelectSingleNode("configuration/userSettings/setting[@name='phoneNumber']"); 

setting.SetAttribute("name", "address"); //this is to change the name attribute value into address 
관련 문제