2012-08-10 12 views
8

मैं एक एक्सएमएल (यह ठीक है कि यह दिखता):एक्सएमएल नोड को बदलने के तरीके को महत्व देता

<PolicyChangeSet schemaVersion="2.1" username="" description=""> 
    <Attachment name="" contentType=""> 
     <Description/> 
     <Location></Location> 
    </Attachment> 
</PolicyChangeSet> 

इस उपयोगकर्ता की मशीन पर है।

मुझे प्रत्येक नोड में मान जोड़ने की आवश्यकता है: उपयोगकर्ता नाम, विवरण, अनुलग्नक नाम, सामग्री प्रकार, और स्थान।

string newValue = string.Empty; 
      XmlDocument xmlDoc = new XmlDocument(); 

      xmlDoc.Load(filePath); 
      XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet"); 
      node.Attributes["username"].Value = AppVars.Username; 
      node.Attributes["description"].Value = "Adding new .tiff image."; 
      node.Attributes["name"].Value = "POLICY"; 
      node.Attributes["contentType"].Value = "content Typeeee"; 

      //node.Attributes["location"].InnerText = "zzz"; 

      xmlDoc.Save(filePath); 

कोई मदद:

यह वही है मैं अब तक किया है?

उत्तर

13

XPath के साथ। XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet"); आपके रूट नोड का चयन करता है।

+1

कि काम किया :) करने के लिए उपयोग ... मैं केवल स्वीकार कर सकते हैं 10 मिनट में आपका जवाब था। थक्स जनवरी! – Testifier

+0

हालांकि मैं "स्थान" के लिए कोई मूल्य कैसे जोड़ूं? यह सिर्फ <> ... के बीच है ?? – Testifier

+0

कभी भी :) XmlNode की 'InnerText' प्रॉपर्टी को देखें। – Jan

0

अपनी SelectSingleNode विधि में, आपको एक XPath अभिव्यक्ति प्रदान करने की आवश्यकता है जिसे आप चुनने के लिए देख रहे हैं। यदि आप Google XPath को इसके लिए कई संसाधन पाएंगे।

http://www.csharp-examples.net/xml-nodes-by-name/

आप प्रत्येक नोड के लिए इन जोड़ने की जरूरत है, तो आप शीर्ष पर शुरू और बच्चों की सब से अधिक पुनरावृति कर सकते हैं।

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.aspx

3

इस के साथ मिल गया -

xmlDoc.Load(filePath); 
      XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet"); 
      node.Attributes["username"].Value = AppVars.Username; 
      node.Attributes["description"].Value = "Adding new .tiff image."; 

      node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment"); 
      node.Attributes["name"].Value = "POLICY"; 
      node.Attributes["contentType"].Value = "content Typeeee"; 
xmlDoc.Save(filePath); 
2

LINQ एक्सएमएल :)

XDocument doc = XDocument.Load(path); 
IEnumerable<XElement> policyChangeSetCollection = doc.Elements("PolicyChangeSet"); 

foreach(XElement node in policyChangeSetCollection) 
{ 
    node.Attribute("username").SetValue(someVal1); 
    node.Attribute("description").SetValue(someVal2); 
    XElement attachment = node.Element("attachment"); 
    attachment.Attribute("name").SetValue(someVal3); 
    attachment.Attribute("contentType").SetValue(someVal4); 
} 

doc.Save(path); 
2
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Description").InnerText = "My Desciption"; 
xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/Location").InnerText = "My Location"; 
0
For setting value to XmlNode: 
XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet"); 
      node["username"].InnerText = AppVars.Username; 
      node["description"].InnerText = "Adding new .tiff image."; 
      node["name"].InnerText = "POLICY"; 
      node["contentType"].InnerText = "content Typeeee"; 

For Getting value to XmlNode: 
username=node["username"].InnerText 
संबंधित मुद्दे