I want to change the values of some Elements but my code isn't working.
I have this XML-File:
<?xml version="1.0" encoding="utf-8"?>
<data>
<application id="1">
<applicationName>Instagram</applicationName>
<username>test</username>
<password>123</password>
<info>test</info>
</application>
</data>
And this C# Code:
string applicationName = "Test";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Data.xml");
XmlNode node = xmlDoc.SelectSingleNode("/data/application[#id='1']/applicationName");
node.InnerText = applicationName;
xmlDoc.Save("Data.xml");
What is the correct code to change the applicationName in the XML-File?
Use LINQ and XDocument:
string applicationName = "Test";
XDocument xdocument = XDocument.Load("Data.xml");
var appName = xdocument.Elements("applicationName").Single();
appName.Value = applicationName;
xdocument.Save("Data.xml");
But you should add System.Xml.Linq to your using directives first.
Related
i use this code for creating xml file using c# class.
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode RootNode = doc.CreateElement("SDF");
doc.AppendChild(RootNode);
XmlAttribute rootAttribute2 = doc.CreateAttribute("Version");
rootAttribute2.Value = "3.0";
RootNode.Attributes.Append(rootAttribute2);
XmlAttribute rootAttribute = doc.CreateAttribute("xmlns:sdf");
rootAttribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
RootNode.Attributes.Append(rootAttribute);
XmlAttribute rootAttribute1 = doc.CreateAttribute("sdf:noNamespaceSchemaLocation");
rootAttribute1.Value = "SDF.xsd";
RootNode.Attributes.Append(rootAttribute1);
output of this code..
<?xml version="1.0" encoding="UTF-8"?>
<SDF Version="3.0" xmlns:sdf="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="SDF.xsd">
but i want output like that
<?xml version="1.0" encoding="UTF-8"?>
<SDF Version="3.0" xmlns:sdf="http://www.w3.org/2001/XMLSchema-instance" sdf:noNamespaceSchemaLocation="SDF.xsd">
You need to use a different overload:
XmlAttribute rootAttribute1 =
doc.CreateAttribute("sdf", "noNamespaceSchemaLocation", null);
Use Overloaded version CreateAttribute method.
doc.CreateAttribute("name","namespaceURI")
See below link for detials.
http://msdn.microsoft.com/en-us/library/System.Xml.XmlDocument.CreateAttribute%28v=vs.110%29.aspx
i searched on google and find answer that solve my problem
XmlNode RootNode = doc.CreateElement("SDF");
doc.AppendChild(RootNode);
XmlAttribute rootAttribute2 = doc.CreateAttribute("Version");
rootAttribute2.Value = "3.0";
RootNode.Attributes.Append(rootAttribute2);
XmlAttribute newAttr = doc.CreateAttribute("sdf", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
newAttr.Value = "SDF.xsd";
RootNode.Attributes.Append(newAttr);
i am using C# windows phone 8 and I have the following XML
<?xml version="1.0" encoding="UTF-8" ?>
<login res="SUCCESS" encstatus="DEFAULT" usedquota="0" />
I need to extract the values of res, encstatus and usedQuota.
How do I do that in xml parsing?
I tried this
XDocument xDoc = XDocument.Parse(str);
var pol = xDoc.Element("res");
var items = xDoc.Descendants("res");
Where str is the xml file, but all the elements are empty / null.
You're trying to get the attribute values, no element:
XDocument xDoc = XDocument.Parse(str);
var pol = (string)xDoc.Root.Attribute("res");
Those nodes are attributes:
XDocument xDoc = XDocument.Parse(str)
XElement login = xDoc.Root;
string res = (string)login.Attribute("res");
string encstatus = (string)login.Attribute("encstatus");
int usedquota = (int)login.Attribute("usedquota");
I try to parse this XML file (config file from Chirpy):
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="urn:ChirpyConfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:ChirpyConfig http://www.weirdlover.com/chirpy/chirp.xsd">
<FileGroup Name="Built.debug.js" Minify="false">
<File Path="jquery/jquery-1.7.2.js"/>
<File Path="jquery.address/jquery.address-1.4.js" />
</FileGroup>
</root>
with this code:
var path = Server.MapPath("~/Scripts/ScriptfilesMashup.chirp.config");
var file = new XPathDocument(path);
var nav = file.CreateNavigator();
var nodes = nav.Select("/root/FileGroup/File");
but nodes is always empty, regardless of how I call the nav.Select method. I barely used XPath before so maybe I'm doing it wrong - but what? Only the selector * gives me the root node.
What would be the selector to get the Path Attribute of all File nodes?
EDIT: SOLUTION
Thanks to Kirill, the final solution looks like this:
var path = Server.MapPath("~/Scripts/ScriptfilesMashup.chirp.config");
var file = new XPathDocument(path);
var nav = file.CreateNavigator();
var ns = "urn:ChirpyConfig";
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace("x", ns);
var nodes = nav.Select("/x:root/x:FileGroup/x:File/#Path", nsMgr);
while(nodes.MoveNext())
{
var path = nodes.Current.Value;
}
It is because elements root, FileGroup and File are defined in urn:ChirpyConfig namespace.
Use this:
XPathDocument xmldoc = new XPathDocument(xmlFile);
XPathNavigator nav = xmldoc.CreateNavigator();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace("x", "urn:ChirpyConfig");
XPathNavigator result = nav.SelectSingleNode("/x:root/x:FileGroup/x:File", nsMgr);
Im using c#.net windows form application. I have a xml file whose name is hello.xml and it goes like this
<?xml version="1.0" encoding="utf-8" ?>
<languages>
<language>
<key>abc</key>
<value>hello how ru</value>
</language>
<language>
<key>def</key>
<value>i m fine</value>
</language>
<language>
<key>ghi</key>
<value>how abt u</value>
</language>
</languages>
How can i get the root node i.e <languages> into a text box. At this time I will have the xml file name. i.e "hello.xml". Using this I should get the root node.
Using LINQ to XML you can do this:
XDocument doc = XDocument.Load("input.xml");
string rootLocalName = doc.Root.Name.LocalName;
textBox1.Text = '<' + rootLocalName + '>';
With XmlDocument you can use this:
XmlDocument doc = new XmlDocument();
doc.Load("input.xml");
string rootName = doc.SelectSingleNode("/*").Name;
Or use the XmlDocument DocumentElement property as shown here:
XmlDocument doc = new XmlDocument();
doc.Load("hello.xml");
string root = doc.DocumentElement.Name;
textBox1.Text = "<" + root + ">";
I have a xml-file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
<level>
<node1 />
<node2 />
<node3 />
</level>
</root>
What is the simplest way to insert values in node1, node2, node3 ?
C#, Visual Studio 2005
Here you go:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(#"
<root>
<level>
<node1 />
<node2 />
<node3 />
</level>
</root>");
XmlElement node1 = xmldoc.SelectSingleNode("/root/level/node1") as XmlElement;
if (node1 != null)
{
node1.InnerText = "something"; // if you want a text
node1.SetAttribute("attr", "value"); // if you want an attribute
node1.AppendChild(xmldoc.CreateElement("subnode1")); // if you want a subnode
}
//Here is the variable with which you assign a new value to the attribute
string newValue = string.Empty
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);
XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element");
node.Attributes[0].Value = newValue;
xmlDoc.Save(xmlFile);
Credit goes to Padrino.
How to change XML Attribute
XElement t = XElement.Load("filePath");
t.Element("level").Element("node1").Value = "";
t.Element("level").Element("node2").Value = "";
t.Element("level").Element("node3").Value = "";
t.Save("filePath");
Use AppendChild method to inser a child inside a node.
yournode.AppendChild(ChildNode);
link text