<?xml version="1.0" encoding="utf-8"?>
<Settings>
<Code>D:picture\simple</Code>
<Password>picture</Password>
</Settings>
I want to change the value which is inside Code and Password.How can I change the value.
XElement root = XElement.Load("path/to/xml/somewhere");
root.Element("Code").Value= "newcode";
root.Element("Password").Value ="Penis *gnhihihi*";
//only in Linqpad
root.Dump();
And there you have it. This is one way to change it.
It is up to you how you save it back to an xml file
Related
Can any friends help on this.
Now I want to read a complete path when I get a value.
<?xml version="1.0" encoding="UTF-8" ?>
<Element xsi:schemaLocation="http://localhost/AML/CaseInvestigationMangement/Moduli/XmlImportControls/xsdBorrow.xsd xsd2009027_kor21.xsd" Kod="370" xmlns="http://localhost/AML/CaseInvestigationMangement/Moduli/XmlImportControls/xsdBorrow.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
/2001/XMLSchema-instance">
<ANode>
<BNode>
<CNode>
<Example>
<Name>John</Name>
<NO>001</NO>
</Example>
</CNode>
</BNode>
<ID>1234</ID>
<Date>2011-10-01</Date>
</ANode>
<ANode>
<BNode>
<CNode>
<Example>
<Name>Mike</Name>
<NO>002</NO>
</Example>
</CNode>
</BNode>
<ID>5678</ID>
<Date>2011-03-31</Date>
</ANode>
</Element>
Now I want to read a complete path when I get a value, like NO element then I want to store complete path NO->Example->Cnode->Bnode->Anode. please help...
I have used XMLTextReader to read xml element and value. for more reference please click I want to read the xml sub sub sub node name and value
With xml linq, you can do it as below:
var doc = XDocument.Load(#"fileUri");
var paths=doc.Descendants()
.Where(e=>!(e.HasElements || String.IsNullOrEmpty(e.Value)))
.Select(e=>e.Name+"->"+
String.Join("->",e.Ancestors().Select(a=>a.Name)));
I am receiving "Object reference not set to an instance of an object." error when I try and save the value to an XML file. I have tried messing around with it for a while now. I know it works cause I use the same code (with slight difference) to save the other values to this file. This is the only one not working.
public void SAVEtxtDestination(string txtFileStuff)
{
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load("C:\\Users\\fthompson11\\WebFile.xml");
XmlNode root = myXmlDocument.DocumentElement;
var targetKey = "Path";
XmlNode node = root.SelectSingleNode(string.Format("Text[#Key = '{0}']", targetKey));
node.Attributes["Value"].Value = txtFileStuff;
myXmlDocument.Save("C:\\Users\\fthompson11\\WebFile.xml");
}
Here is what my XML file looks like:
<?xml version="1.0" encoding="utf-8"?>
<!--This is to write the connection strings, text file location, and report destination.-->
<AdminPaths>
<AdminPath Name="sqlConnection1" connectionString="tacohell" />
<TextPath>
<Text Key="Path" Value="Test3" />
<Text Key="Report" Value="Test2" />
</TextPath>
</AdminPaths>
In your comment you say indicate a lower case 'v' in value nodes.attributes["value"].Value in the example it's upper upper case. Linq to XML is case sensitive, make sure the attribute name in code matches exactly with the attribute name in the file.
I'm trying to get the value of the Address element from the following XML text, but it's not finding it unless I remove xmlns="http://www.foo.com" from the Root element. However, the XML is valid even with it. What's the problem here?
Since I'm getting the XML text from a web service, I don't have control over it, but I can strip out the xmlns part if I have to as the last resort.
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.foo.com">
<Address>Main St SW</Address>
</Root>
var doc = XDocument.Parse(xmlTextAbove);
var address = doc.Descendants().Where(o => o.Name == "Address").FirstOrDefault();
Console.WriteLine(address.Value); // <-- error, address is null.
As your xml contains a namespace, you have to mention that in code. This will work:
XNamespace nsSys = "http://www.foo.com";
XElement xDoc = XElement.Load("1.xml");
XElement xEl2 = xDoc.Descendants(nsSys + "Address").FirstOrDefault();
However I had to change your xml a little bit, as it contained repeated xmlns:xsi and xmlns:xsd which should occur only once per xml format:
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.foo.com" >
<Address>Main St SW</Address>
</Root>
Related article in MSDN: XNamespace Class
The document root's XML namespace is included in the textual representation of o.Name, which is actually an instance of XName so the condition never matches.
The easiest fix is to use LocalName for the comparison:
.Where(o => o.Name.LocalName == "Address")
I am novice and struggling in some XML operations Like open and Delete. I have done the Add part.
Partys.xml
<?xml version="1.0" encoding="utf-8"?>
<Partys>
<Customers>
<Customer CustomerID="1">
<PersonalName>
<LastName>Baker</LastName>
<FirstName>Eugene</FirstName>
</PersonalName>
<Citizenship>Africa</Citizenship>
</Customer>
<Customer CustomerID="2">
<PersonalName>
<LastName>Baker</LastName>
<FirstName>Eugene</FirstName>
</PersonalName>
<Citizenship>Africa</Citizenship>
</Customer>
</Customers>
</Partys>
Q: I want to open the node detail customer where CustomerID (Attribute) is 1. What is the C# code for this?
Q: I want to delete the node customer where CustomerID (Attribute) is 2. What is the C# code for this?
You could try something like this:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Parties.xml");
XmlNode t = xmlDoc.SelectSingleNode("/Partys/Customers/Customer[#CustomerID='2']");
t.ParentNode.RemoveChild(t);
xmlDoc.Save();
Once you have t, you can do whatever you want with it including show it in the Console (by accessing the various properties)
Here, we have deleted the node and saved back to file, but you could do whatever you want with the XmlDocument....
I'm attempting to read a XML template, code below, and put this content to a new XML file.
<?xml version="1.0" encoding="utf-8"?>
<Format>
<Name title=""></Name>
<FormatA>
<Scheme name="A" set="number" get="integer">
<Sample set="number" get="integer">33</Sample>
</Scheme>
</FormatA>
<FormatX>
<Scheme name="A" set="number" get="integer">
<Sample set="number" get="integer">44</Sample>
</Scheme>
</FormatX>
</Format>
And will input values and write the title and a value for name. e.g When I enter "Counter" for the title and "Cnt" for the value. It will write
<Name title="Counter">Cnt</Name>
Basically, I'll just copy the contents of the template, put it in the new XML file with the inputted title and value.
Thank you for reading and who will answer my inquiry.
Did you tried string.Format?
Create the Template XML file with
Place Holders, like this {1}
Call
String.Format(XmlContent,"Counter","Cnt")
Hope it helps.