how i can select ip and port elements form this xml file by linq to xml
<?xml version="1.0" encoding="utf-8"?>
<settings>
<IncomingConfig>
<ip>10.100.101.18</ip>
<port>5060</port>
</IncomingConfig>
<Device>
<username>tarek</username>
<AgentName>tarek</AgentName>
<password>ffff</password>
</Device>
<Device>
<username>adf</username>
<AgentName>adf</AgentName>
<password>fadsf</password>
</Device>
</settings>
and i write this code but not work
XDocument doc = XDocument.Load(CONFIGURATION_FULL_PATH);
var port = int.Parse(doc.XPathSelectElement("port").Value);
var localIpAdres = doc.XPathSelectElement("ip").Value;
If you have loaded your file into the doc variable you simply need
string localIpAddress = doc.Root.Element("IncomingConfig").Element("ip").Value;
string port = doc.Root.Element("IncomingConfig").Element("port").Value;
Try
doc.XPathSelectElement("//port").Value
The argument must be an XPath expression.
Related
I have a c# console application where I update, add and delete information from an xml which works so far. But when i show the xml in my console with WriteLine it shows like this:
<ArrayOfKunde xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Kunde id="1">
<firstName>Hasan 2</firstName>
<lastName>sad</lastName>
<adress>sdfd</adress>
<birthday>vcxbgf</birthday>
<bankDetails>bcgh</bankDetails>
</Kunde>
<Kunde id="2">
<firstName>ghf</firstName>
<lastName>nbv</lastName>
<adress>bjk</adress>
<birthday>hjvn</birthday>
<bankDetails>jhgj</bankDetails>
</Kunde>
<Kunde id="3">
<firstName>mbn,</firstName>
<lastName>hgj</lastName>
<adress>ghj</adress>
<birthday>ghjg</birthday>
<bankDetails>hghj</bankDetails>
</Kunde>
</ArrayOfKunde>
it is probably due to my code to print it:
string filepath = "customerdatabase2.xml";
var xDoc = XDocument.Load(filepath);
Console.WriteLine(xDoc);
I wanted to ask if there is any way for me to filter what is shown? For example I do not want to show:
<ArrayOfKunde xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
Use: XDocument.Descendants
So your code can be:
string filepath = "customerdatabase2.xml";
var xDoc = XDocument.Load(filepath);
foreach (var node in xDoc.Descendants().Skip(1))
Console.WriteLine(node);
But this is very much tied to your Xml structure. Note that I am skipping 1st descendant because that will be the Top level ArrayOfKunde node.
I have the following XML-File.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Contacts>
<Contact>
<Name>Example</Name>
<PhoneNo>0481-12345678</PhoneNo>
<SalesPhoneNo>310<SalesPhoneNo />
</Contact>
</Contacts>
Would it be possible to search for the PhoneNo 0481-12345678 in the XML and then only show the SalesPhoneNo 310.
You could use an XPath query to do this. For example,
var xml = #"<Contacts>
<Contact>
<Name>Example</Name>
<PhoneNo>0481-12345678</PhoneNo>
<SalesPhoneNo>310</SalesPhoneNo>
</Contact>
</Contacts>";
XDocument xDoc = XDocument.Parse(xml);
// this will return 310
string phoneNo = '0481-12345678';
var salesPhoneNo = xDoc.XPathEvaluate("string(//Contact[PhoneNo='" + phoneNo + "']/SalesPhoneNo)");
Note you need to add a using System.Xml.XPath to use XPath query on an XDocument
I have an XML document I am trying to query with LINQ to XML. The XML is ..
<?xml version="1.0" encoding="UTF-8"?>
<response>
<operation>
<authentication>
<username>redacted</username>
<isauthenticated>true</<isauthenticated>>
</authentication>
<result>
<status>success</status>
<other-elements></<other-elements>
<other-elements></<other-elements>
<other-elements>
<other-sub-elements></other-sub-elements>
<other-sub-elements></other-sub-elements>
</<other-elements>
</result>
</operation>
</response>
I am trying to read the value of the node <status> to determine if the API call was successful. I am having trouble putting together the LINQ syntax necessary to grab the value of the <status node. I thought I could use XPath syntax as such to grab the value.
XDocument xml = XDocument.Parse(xmlResponse);
string returnValue = = xml.Descendants("result/status").Select(x => x.Name).ToString();
However, I am getting the following error ..
The '/' character, hexadecimal value 0x2F, cannot be included in a name.
Try this code:
XDocument xdoc = XDocument.Parse(#"
<response>
<operation>
<authentication>
<username>redacted</username>
<isauthenticated>true</isauthenticated>
</authentication>
<result>
<status>success</status>
</result>
</operation>
</response>
");
Boolean isSuccess;
String s = xdoc.Element("response").Element("operation").Element("result").Element("status").Value;
isSuccess = s == "success";
It gets the value of the status element and checks whether it is equal to a specific value; in this case, isSuccess will be true.
The LINQ-to-XML methods Elements() and Descendants() only deal with single names, not xpath-like paths. If you want to give an xpath expression, use the xpath extensions.
// using System.Xml.Linq;
var status = (string)xml.XPathSelectElement("//result/status");
Otherwise you need to build up an equivalent query using the methods correctly.
var status = (string)xml.Descendants("result").Elements("status").FirstOrDefault();
Hi because of a misunderstanding I want to ask my question again.
I have the following XML structure:
<?xml version="1.0" encoding="utf-8"?>
<xml>
<root>
<Item>
<taxids>
<string>330</string>
<string>374</string>
<string>723</string>
<string>1087</string>
<string>1118</string>
<string>1121</string>
</taxids>
</Item>
</root>
</xml>
I need to get all the string nodes from the xml file to a string variable.
I want to get a string like this:
<taxids><string>330</string><string>374</string><string>723</string><string>1087</string><string>1118</string><string>1121</string></taxids>
My linq to xml:
var query = from ip in doc.XPathSelectElements("xml/root/Item")
select ip.XPathSelectElement("taxids").ToString();
But I am getting the following in one row of the variable query:
"System.Xml.XPath.XPathEvaluator+<EvaluateIterator>d__0`1[System.Xml.Linq.XElement]"
Is this possible?
Thanks!
Try this:
var result = doc.Element("xml")
.Element("root")
.Element("Item")
.Element("taxids")
.ToString(SaveOptions.DisableFormatting);
// result == "<taxids><string>330</string><string>374</string> ... </taxids>"
I have two XML file sitemap.xml and mouse.xml which look like below.Here the thing is that
i need to compare sitemap.xml with mouse.xml in such a way that the tag
<Name></Name>.I need to compare both xml file whether the content
coming inside <Name></Name> tag is same or not in c# code
Here the <Name></Name> tag are different means sitemap.xml contain "test
" and mouse.xml contain "exam".
<?xml version="1.0" standalone="yes"?>
<ObjectClass>
<Image>00000000-0000-0000-0000-000000000000</Image>
<Description />
<Name>test</Name>
<DefaultApp>00000000-0000-0000-0000-000000000000</DefaultApp>
<ID>464930eb-e518-4d0c-b80b-184c97c7dd27</ID>
<ParentClassID>00000000-0000-0000-0000-000000000002</ParentClassID>
<DynamicPopulation>false</DynamicPopulation>
<TimeoutPeriod>0</TimeoutPeriod>
<Persist>false</Persist>
<ClassVersion>1</ClassVersion>
<Reinitialize>false</Reinitialize>
</ObjectClass>
this is mouse.xml
<?xml version="1.0" standalone="yes"?>
<ObjectClass>
<Image>00000000-0000-0000-0000-000000000000</Image>
<Description />
<Name>exam</Name>
<DefaultApp>00000000-0000-0000-0000-000000000000</DefaultApp>
<ID>464930eb-e518-4d0c-b80b-184c97c7dd27</ID>
<ParentClassID>00000000-0000-0000-0000-000000000002</ParentClassID>
<DynamicPopulation>false</DynamicPopulation>
<TimeoutPeriod>0</TimeoutPeriod>
<Persist>false</Persist>
<ClassVersion>1</ClassVersion>
<Reinitialize>false</Reinitialize>
</ObjectClass>
Try the Microsoft XML diff API.
Try,
XmlDocument doc1 = new XmlDocument();
XmlDocument doc2 = new XmlDocument();
doc1.Load(#"c:\myproject\WindowsApplication1\sitemap.xml");
doc2.Load(#"c:\myproject\WindowsApplication1\mouse.xml");
XmlNodeList a = doc1.GetElementsByTagName("Name");
XmlNodeList b = doc2.GetElementsByTagName("Name");
if (a.Count == 1 && b.Count == 1)
{
if (a[0].InnerText == b[0].InnerText)
Console.WriteLine("Equal");
else
Console.WriteLine("Not Equal");
}
XMLUnit is awesome for xml comparison. Primarily Java based but there is a .Net port there too (I've only used the Java one): http://xmlunit.sourceforge.net/