Okay so this is very basic but I've literally started learning how to read an XML document today and i usually find answers more comprehensive on here than on online guides. Essentially i'm coding a Pokemon game which uses an XML file to load all the stats (its one i swiped from someone else).The user will input a Pokemon and i then want to read the Base Stats of the Pokemon from the XML file, to give a template, this would be one of the Pokemon:
<Pokemon>
<Name>Bulbasaur</Name>
<BaseStats>
<Health>5</Health>
<Attack>5</Attack>
<Defense>5</Defense>
<SpecialAttack>7</SpecialAttack>
<SpecialDefense>7</SpecialDefense>
<Speed>5</Speed>
</BaseStats>
</Pokemon>
The code ive tried to use is:
XDocument pokemonDoc = XDocument.Load(#"File Path Here");
while(pokemonDoc.Descendants("Pokemon").Elements("Name").ToString() == cbSpecies.SelectedText)
{
var Stats = pokemonDoc.Descendants("Pokemon").Elements("BaseStats");
}
but this just returns pokemonDoc as null, any idea where im going wrong?
NOTE:
cbSpeciesSelect is where the user selects which species of pokemon they want.
The File Path definitely works as i've used it already in my program
The while loop never actually starts
Try xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var pokemon = doc.Descendants("Pokemon").Select(x => new {
name = (string)x.Element("Name"),
health = (int)x.Element("BaseStats").Element("Health"),
attack = (int)x.Element("BaseStats").Element("Attack"),
defense = (int)x.Element("BaseStats").Element("Defense"),
specialAttack = (int)x.Element("BaseStats").Element("SpecialAttack"),
specialDefense = (int)x.Element("BaseStats").Element("SpecialDefense"),
speed = (int)x.Element("BaseStats").Element("Speed"),
}).FirstOrDefault();
}
}
}
Can you try below code:
foreach(var e in pokemonDoc.Descendants("Pokemon").Elements("Name"))
{
if(e.Value==cbSpecies.SelectedText)
{
var Stats = pokemonDoc.Descendants("Pokemon").Elements("BaseStats");
}
}
Related
Below is my code to create a directory in my PC.
using System;
using System.IO;
using System.Text;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo dataDir = new DirectoryInfo(#"C:\CsharpData");
Console.WriteLine(dataDir.Attributes);
Console.ReadLine();
}
}
}
But, the result looks like this.
Attribute is equal to -1, and I can't get my desired directory.
Can anyone let me know what my mistake is?
Use below code. You need to use create ,ethod for creating the directory.
DirectoryInfo dataDir = new DirectoryInfo(#"C:\CsharpData");
if(!dataDir.Exists)
{
dataDir.Create();
}
System.Console.WriteLine(dataDir.Attributes);
System.Console.ReadLine();
I have an XML-encoded attribute value. This is actually from a processing instruction. So the original data looks something like this:
<root><?pi key="value" data="<foo attr="bar">Hello world</foo>" ?></root>
I can parse it like this:
using System;
using System.Linq;
using System.Xml.Linq;
public class Program
{
private const string RawData = #"<root><?pi key=""value"" data=""<foo attr="bar">Hello world</foo>"" ?></root>";
public static void Main()
{
XDocument doc = GetXDocumentFromProcessingInstruction();
IEnumerable<XElement> fooElements = doc.Descendants("foo");
// ...
}
private static XProcessingInstruction LoadProcessingInstruction()
{
XDocument doc = XDocument.Parse(rawData);
return doc
.DescendantNodes()
.OfType<XProcessingInstruction>()
.First();
}
private static XDocument GetXDocumentFromProcessingInstruction()
{
XProcessingInstruction processingInstruction = LoadProcessingInstruction();
// QUESTION:
// Can there ever be a situation where HtmlDecode wouldn't decode the XML correctly?
string decodedXml = WebUtility.HtmlDecode(processingInstruction.Data);
// This works well, but it contains the attributes of the processing
// instruction as text.
string dummyXml = $"<dummy>{xml}</dummy>";
return XDocument.Parse(dummyXml);
}
This works absolutely fine, as far as I can tell.
But I am wondering if there might be some edge cases where it may fail, because of differences in how data would be encoded in XML vs. HTML.
Anybody have some more insight?
Edit:
Sorry, I made some incorrect assumptions about XProcessingInstruction.Data, but the code above was still working fine, so my question stands.
I have nonetheless rewritten my code and wrapped everything in an XElement, which (of course) removed the issue altogether:
private static XDocument GetXDocumentFromProcessingInstruction2()
{
XProcessingInstruction processingInstruction = LoadProcessingInstruction();
string encodedXml = string.Format("<dummy {0} />", processingInstruction.Data);
XElement element = XElement.Parse(encodedXml);
string parsedXml = element.Attribute("data").Value;
return XDocument.Parse(parsedXml);
}
So this does exactly what I need. But since WebUtility.HtmlDecode worked sufficiently well, I would still like to know if there could have been a situation where the first approach could have failed.
Removing the question marks and adding a forward slash at end of your input I got this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = "<pi data=\"<foo attr="bar">Hello world</foo>\" />";
XElement pi = XElement.Parse(input);
string data = (string)pi.Attribute("data");
XElement foo = XElement.Parse(data);
string attr = (string)foo.Attribute("attr");
string innertext = (string)foo;
}
}
}
What do you think the correct XPath is to pull the dublin core identifier below?
I added a namespace manager with these entries:
// Add the namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(m_xml.NameTable);
nsmgr.AddNamespace("mets", "http://www.loc.gov/METS/");
nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
nsmgr.AddNamespace("dcterms", "http://purl.org/dc/terms/");
And I have tried 15 or so different XPath iterations including the ones below. When I do not get an error, the result is null.
//xml_uuid = m_xml.SelectSingleNode("/mets:mets/mets:dmdSec/mets:mdWrap/mets:xmlData/dcterms:dublincore/dc:identifier").Value;
xml_uuid = m_xml.SelectSingleNode("//dc:identifier",nsmgr).Value;
Here is the xml I am working with:
<mets:mets xmlns:mets="http://www.loc.gov/METS/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/version18/mets.xsd">
<mets:metsHdr CREATEDATE="2017-03-08T20:13:27" />
<mets:dmdSec ID="dmdSec_1">
<mets:mdWrap MDTYPE="DC">
<mets:xmlData>
<dcterms:dublincore xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xsi:schemaLocation="http://purl.org/dc/terms/ http://dublincore.org/schemas/xmls/qdc/2008/02/11/dcterms.xsd">
<dc:identifier>F2015.5</dc:identifier>
</dcterms:dublincore>
</mets:xmlData>
</mets:mdWrap>
</mets:dmdSec>
etc...
I am trying to assign the dc:identifier - F2015.5 in this case - to a string.
With xml linq and ignoring the namespaces :
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication131
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
string results = (string)doc.Descendants().Where(x => x.Name.LocalName == "identifier").FirstOrDefault();
}
}
}
Here is one working answer. I believe the main thing is to use .InnerText and not .Value.
xml_uuid = m_xml.SelectSingleNode("//mets:xmlData[1]/dcterms:dublincore[1]/dc:identifier[1]", nsmgr).InnerText;
I have this XML File
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Mobiles>
<Mobile ID="98a2d0b9c2661725a7486423ebd94abdef53ed89051ff5ac7a9c63a4b2a99d7a" RegDate="26/05/2017">
<FullName>Galaxy S-4</FullName>
<Vendor>Samsung</Vendor>
<Date>2010</Date>
<Price>30000SYP</Price>
<Weight>5g</Weight>
<Features>All</Features>
<Notifecations></Notifecations>
<Picture>/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCADCAQQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZ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if4M6lbzvPoTrd2rcrDIwWVPbJ4YfkfaveaSmpNbCaufJtx4G8TWjun9j6hH82N3llQ30PTvWdd+E9ah/1un3LP/s/O/wCS5NfYlFae2YuVHxqmg61D9zSdQXdwy/ZZAGGQ2CNvIyqnB7gelKdMv0+/o8//AIDyj+VfZFGKTq36Bynx4lrqX/QMnX/t3f8ArmrcGlak/wA/9n3zf9u7n+lfW2xf7q06j2r7BynytaaVqTyfurG5V/8AahZD/wCPAVoReG9fvJ9q6PqEjfd3fZ2I/wC+jwPzr6aoo9qxciPF/Dfwpv7idLjXkS0tV5a3Vg0r+xIyqj3BJ+nWvYoYo4YUiiVViRQqqvQAdAKmpKhyb3KSSFrzr4lfDC18cwJeW8q2eswLsjmZfklXqFfHOM9GHIyeD0r0WikM+PtU+G3jjQpmWXQbydS3+sso/PVvf5MkD6gVlnTNdi/4+NP1KD/f09/8K+1aK09oyeVHxOIbr+N51/7cWp6QzvIqLdy7mbaq/YXJY+gAHJr7VpMU/a+QcqPi0W100mxLiRnZtqr9hkzn0A29aPJuPu/a5d33dv2GT/CvtKil7RhynxcIL1/9VLct/u6e9S/2N4im/wBVp+sy/wDXPTXGfxxX2bRR7Rhyo+RNH+FHjPWpF8rRZ7RGPzTaj+5C+5B+b8lNfQnw8+Hdh4E06QJJ9p1K5A+03TLjgdEUdlH5k8nsB21LUuTY0rBRRRUjCiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD//2Q==</Picture>
</Mobile>
<Mobile ID='00283f5aa9c0c68c342c02977051757a7faae0ee70b086e628da7cc5baef2100' RegDate="26/05/2017">
<FullName>6600</FullName>
<Vendor>Nokia</Vendor>
<Date>2006</Date>
<Price>200SYP</Price>
<Weight>10g</Weight>
<Features>blutooth</Features>
<Notifecations></Notifecations>
<Picture>/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw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1ln8rUNb124YpuIk1F40wOvCkCpNP8L+F2gS+tbKzuFz8lyxFwc467mJqDxDZ32r6YLN9MaMS7oGaO4ix8wwQPm+XIB46ZwOM1D4c0G/0nRrvTJZ7ZpLgeWJGcAqFXb9xC2WxjJyOg9KAJvEFtLf+F9SEIjtrY2zNH5i/NJjDLx/CMgdeT6d62PhDfrd+EZoVMm22u3jjWU/MEIDD+Z/KsuUfboSqBtQcoMvMV8pCR02/dUjtklvTNN+DMjNHrELdN8cg/X/AAFDGtz1eiiipLCiiigDwD4iWP8AY3iW5hK/6Nct9rhGOzffH5lvzFcvoGuWuga/p8UiyR/Z5CjyRSMrSRv1yQc4HysOexr2b4s+Hf7X8JSXtuha80wNOm0ZZkx86j8ADj/ZFfO+pD7ZpsN9GoLwYSTjqvb+opkM+gXbNvJbtNqKsAGyiTEGUEHn5TgZA7k1EjLOwaKxup4ZMMBL5mCCMnIfC5zWH4F12LXdGtFbUFXUYRtkjlUZIUYVvvLuyAB3II5rZuLYQtMbu4XABBZI1QBQdyjLMw/j447UIRVJZ4bZLxyyoqoLWJiSMEDAIGA3HRQTxwRUXwbONS1ePttU/r/9eql5rdtp8Ej2BZ5A5IumYnGTnAfoRyRtQYOOcVo/CCxljn1W7ZCiMETkdCecfXGD/wACFNlI9ZoooqSgooooAayhhggEHqD3r5v8b+CbzwZrdzdWlk914fvCchRuEQPJVvTHY9MY719I0YHpQJo+U9K0S3uQJrSe8aBvuoNPkmz+KjFdvpXhvUxtNroepTt03C0itQP+BSkH8ga92ooFynmtn4B1K6nWbUJbWyUf3P8ASZ/wZgFX8FNd5pum22lWK2topWMckscsxPUk9yfWrtFBQtFFFAH/2Q==</Picture>
</Mobile>
</Mobiles>
When I try to remove Mobile element depending on IDtextbox.text rather than static value in XPath expression it doesn't execute
I'm using Windows application in Visual Studio 2010, C# language
I need two examples:
1- Depending on an element inside the parent element.
2- depending on an attribute of the parent element, make sure that attribute is not an integer value, it's code.
Notice: I have searched for many websites and view multiple solutions nobody mention about using control value in XPath expressions.
Use xml linq which is better in this case than xpath
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> mobiles = doc.Descendants("Mobile").ToList();
mobiles = mobiles.Where(x => (string)x.Element("FullName") != "Galaxy S-4").ToList();
mobiles = mobiles.Where(x => (string)x.Attribute("RegDate") != "26/05/2017").ToList();
doc.Descendants("Mobiles").FirstOrDefault().ReplaceWith(new XElement("Mobiles", mobiles));
}
}
}
thanks, Mr.jdweng
I solve the problem with this code
XDocument doc = XDocument.Load(#"Mobiles.xml");
List<XElement> mobiles = doc.Descendants("Mobile").ToList();
//mobiles = mobiles.Where(x => (string)x.Element("ID") == ID).ToList();
mobiles = mobiles.Where(x => (string)x.Attribute("ID") == ID).ToList();
doc.Descendants("Mobile").FirstOrDefault().ReplaceWith();
Best regards
im trying to get some information from a website (http://wowhead.com) now most if it is easy enough with html agility pack, but im stuck on how to get information after a java script tag.
The source what im trying to get info from is
<script type="text/javascript">//<![CDATA[
Markup.printHtml("[ul][li]Level: 49[/li][li]Requires level 47[/li][li]Loremaster: [url=/achievement=4931]Felwood[/url][/li][li]Side: [span class=icon-horde]Horde[/span][/li] [li][icon name=quest_start]Start: [url=/npc=48127]Darla Drilldozer[/url][/icon][/li][li] [icon name=quest_end]End: [url=/npc=48127]Darla Drilldozer[/url][/icon][/li] [li]Sharable[/li][li]Difficulty: [color=r2]47[/color][small] [/small][color=r3]52[/color][small] [/small][color=r4]59[/color][/li][li]Added in patch 4.0.3[/li][/ul]", "sdhafcuvh0", { allow: Markup.CLASS_STAFF, dbpage: true });
//]]></script>
Now from all that, the only thing im interested in is the info from
[url=/npc=48127]Darla Drilldozer[/url]
From which i only want do display 48127 and Darla Drilldozer.
Is there anyway to do this?
Here is an example of my current code in a console to show what kind of thing im after
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using HtmlAgilityPack;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Enter the Quest ID and set it as a WoWHead link
Console.WriteLine("Enter quest ID");
string ID = Console.ReadLine();
Console.WriteLine("Gathering Quest information from: http://www.wowhead.com/quest=" + ID);
//Load WoWHead and search for the quest name in <h1>
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://wowhead.com/quest=" + ID);
HtmlNodeCollection Qname = doc.DocumentNode.SelectNodes("//h1");
//Set QuestName as the second <h1> tag
string QuestName = (Qname[1].InnerText);
//Display information recivied
Console.WriteLine("Quest ID: " + ID);
Console.WriteLine("Quest Name: " + QuestName);
Console.WriteLine("Quest Giver: " );
Console.WriteLine("Quest Giver ID: ");
Console.ReadLine();
}
}
}
So the information needed for Quest giver and Quest giver ID are from the above Javascript.
Is there any way to get this information?
There are many ways to skin a cat, and one of them in this case, is to find the position of the word you are looking for and use a simple string.substring. would that work for you?