I have XML file and the elements/attributes names have “:” character, how I can update its vales?
<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
<my:ECR my:NoOfAutho="16" my:hideDetails="0" my:Type="ECR" my:NoOfVBUCMApprovales="10" >
<my:ECRNo>148</my:ECRNo>
<my:Stage>Approved</my:Stage>
<my:Details>
<my:ReasonForCR>Reason For CR</my:ReasonForCR>
<my:AreaAffected_Publications_VBUCM>false</my:AreaAffected_Publications_VBUCM>
<my:AreaAffected_Engineering>true</my:AreaAffected_Engineering>
<my:AreaAffected_Production>false</my:AreaAffected_Production>
<my:AreaAffected_CustomerSupport>true</my:AreaAffected_CustomerSupport>
<my:AreaAffected_VBUCMTest>false</my:AreaAffected_VBUCMTest>
</my:AreaAffectedVB_UCM>
Your XML sample is invalid as shown. The
myprefix is not defined in the XML.If your XML contained
xmlns:my="schemas.microsoft.com/office/infopath/2003/myXSD/…"then the XML would at least have some hope of being valid.For manipulating XML with namespaces in .NET code, consider using Linq
XDocumentinstead ofXmlDocument. I have found Linq’sXNamespaceandXNametypes to be much, much easier to use with theXDocumentfamily of classes than the old styleXmlDocument‘s rather clunky handling of namespaces.Change your XML to add the
xmlns:myattribute to the root element:In your C# code, add a reference to the Linq stuff to the top of your source file:
Then use code like this (not checked, may contain syntax typos) to load the xml and access the element:
You can then read or modify the properties, attributes, and children of the MNO element.
To read the value of
<MNO>100</MNO>, useMNO_Element.Value.To write a new value to the element, assign to the value property:
MNO_Element.Value = "120";.Single()asserts that there is exactly one node that matches the selection criteria, similar to the.SelectSingleNode()function ofXmlDocument.As you can see from this code, the name of the “my” namespace prefix in the XML document is immaterial to the code that processes the XML – it’s the URI that the “my” prefix represents that is what is important. The prefix is just shorthand so the the XML writer doesn’t have to write long and laborious URIs everywhere.
Writing your XML processing code to be agnostic of the XML namespace prefix is very important because the prefix name can (and will) vary from one XML doc to the next, but the namespace URI will be the same.