I'm trying to parse the following:
<?xml version="1.0" encoding="utf-8"?>
<GC>
<CREATED>01/23/2014 16:10:18</CREATED>
<DATA>
<CONTAINER name="home" type="xml" version="1.1.0.0">
<HEADER>
<ATTRIBUTE name="lang" value="EN" />
<ATTRIBUTE name="destination" value="UK" />
</HEADER>
</CONTAINER>
</DATA>
</GC>
How do I go about finding the value when name="lang"?
So far I have this:
XmlDocument Doc = new XmlDocument();
Doc.Load(#path);
XmlNode node = Doc.DocumentElement.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE/NAME");
string SI = node.Attributes["lang"].InnerText;
Doesn't seem to work unfortunately, could use some help. Many thanks.
This will do it:
XmlNode node =
Doc.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE[#name = 'lang']/#value");
string SI = node.InnerText;
And I would advise using a null check:
XmlNode node =
Doc.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE[#name = 'lang']/#value");
string SI = null;
if(node != null)
{
SI = node.InnerText;
}
With using LINQ to XML you can get it like this:
XDocument xDoc = XDocument.Load("path");
var element = xDoc.Descendans("ATTRIBUTE").First();
var nameAttribute = (string)element.Attribute("name");
This will get you the value of the attribute in the ATTRIBUTE tag which has name == lang:
XmlDocument Doc = new XmlDocument();
Doc.Load(#path);
XmlNode node = Doc.DocumentElement.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE[#name='lang']");
string SI = node.Attributes["value"].InnerText;
XmlNode node = Doc.DocumentElement.SelectSingleNode("/GC/DATA/CONTAINER/HEADER/ATTRIBUTE[#name='lang']");
string SI = node.Attributes["value"].Value;
Related
I have an xml file that has a bunch of channels, and I want to append a channel category to every single one of them. Depending on what channel it is. I'm very new to this so please excuse me if this is an obvious error.
example:
<channel-category>Entertainment</channel-category>
or
<channel-category>News</channel-category>
I have tried the following:
string path;
string xmlfile = "/channels.xml";
path = Environment.CurrentDirectory + xmlfile;
if (exists("channelname1"))
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode root = doc.DocumentElement;
XmlNode node = root.SelectSingleNode("list/channel[#id='channelname1'");
XmlNode category = doc.CreateElement("channel-category");
category.InnerText = "channelcataegorygoeshere";
node.AppendChild(category);
doc.DocumentElement.AppendChild(node);
}
else
{
Console.WriteLine("not found");
}
Console.ReadKey();
}
static bool exists(string channelname)
{
string path;
string xmlfile = "/channels.xml";
path = Environment.CurrentDirectory + xmlfile;
XDocument xmlDoc = XDocument.Load(path);
bool doesexists = (from data in xmlDoc.Element("list").Elements("channel")
where (string)data.Attribute("id") == channelname
select data).Any();
return doesexists;
}
but it's giving me the following error and I can't figure it out.. What am I doing wrong?
An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.dll
Additional information: 'list/channel[#id='channelname1'' has an invalid token.
from this line
XmlNode node = root.SelectSingleNode("list/channel[#id='channelname1'");
My XML looks like this
<?xml version="1.0" encoding="UTF-8"?>
<list info="list">
<channel id="channelname1">
<display-name lang="en">channelname1</display-name>
<icon src="http://locationtologo.com/" />
<url>http://someurl.com</url>
</channel>
<channel id="channelname2">
<display-name lang="en">channelname2</display-name>
<icon src="http://locationtologo.com/" />
<url>http://someurl.com</url>
</channel>
<channel id="channelname3">
<display-name lang="en">channelname3</display-name>
<icon src="http://locationtologo.com/" />
<url>http://someurl.com</url>
</channel>
<channel id="channelname4">
<display-name lang="en">channelname4</display-name>
<icon src="http://locationtologo.com/" />
<url>http://someurl.com</url>
</channel>
</list>
You dont have closing bracket in list/channel[#id='channelname1'(HERE).
Moreover, you are actually trying to do following:
var doc = new XmlDocument();
doc.Load(Environment.CurrentDirectory + "\\channels.xml");
var nodes = doc.SelectNodes("list/channel[#id=\"channelname1\"]");
if (nodes != null)
{
foreach (XmlNode node in nodes)
{
var el = doc.CreateElement("channel-category");
el.InnerText = "SomeValue";
node.AppendChild(el);
}
}
bool doesexists = (from data in xmlDoc.Element("tv").Elements("channel")
where (string)data.Attribute("id") == channelname
select data).Any();
You are trying to reach the channel node where the id equals the channelname inside tv. The problem is that tv does not exist, the channels are inside this:
<list info="list">
Solution: either put the channels into tv, or use a selector appropriate to your current structure.
Why you are using tv instead of list thats why xml library not getting your path of your elements and throwing this error.
try this..
XmlNode node = root.SelectSingleNode("list/channel");
node.Attributes["id"].Value=="channelname1"?true:false;
From the following xml:
<response>
<content>
<Result xmlns="http://www.test.com/nav/webservices/types">
<Name>Test</Name>
</Result>
</content>
<status>ok</status>
</response>
I am trying to get the value of the Name element the following way but that does not work:
private static void Main()
{
var response = new XmlDocument();
response.Load("Response.xml");
var namespaceManager = new XmlNamespaceManager(response.NameTable);
namespaceManager.AddNamespace("ns", "http://www.test.com/nav/webservices/types");
Console.WriteLine(response.SelectSingleNode("/response/content/Result/Name", namespaceManager).InnerXml);
}
How can I select the Name element?
Your code would have worked just fineif the Xml had defined the namespace with a "ns:" prefix.
But in this case, the namespace is given without any prefix, which sets the default namespace for everything in the Result tag to ".../webservice/types".
To reflect this, you need to modify the Xpath, and tell the XmlDocument that the nodes you are looking for under Resultare in the webservice/types namespace. So your query will look like this:
Console.WriteLine(response.SelectSingleNode(#"/response/content/ns:Result/ns:Name", namespaceManager).InnerXml);
For getting directly the text value of a node there is a text() function, if used in the query it would look like:
/response/content/Result/Name/text()
Try this:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = "<response><content><Result xmlns=\"http://www.test.com/nav/webservices/types\"><Name>Test</Name></Result></content><status>ok</status>";
string elementValue = String.Empty;
if (xmlDoc != null)
{
xNode = xmlDoc.SelectSingleNode("/Result");
xNodeList = xNode.ChildNodes;
foreach (XmlNode node in xNodeList)
{
elementValue = node.InnerText;
}
}
I am trying to create an XML file using XmlDocument that will then be passed to an API. Most of the data outputs correctly however there is one element that does not, no matter what I try.
Here is my current output
<agentInventoryReference xmlns="demo.org.uk/demo/AgentInventoryReference"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<agentInventoryIdentity xmlns="">
<shed xmlns="demo.org.uk/demom/AgentInventoryIdentifier">TAX</shed>
<arrivalPort xmlns="demo.org.uk/demo/AgentInventoryIdentifier">LHR</arrivalPort>
<masterAirwayBillPrefix xmlns="demo.org.uk/demo/AgentInventoryIdentifier">125</masterAirwayBillPrefix>
<masterAirwayBillNumber xmlns="demo.org.uk/demo/AgentInventoryIdentifier">12345678</masterAirwayBillNumber>
<nominatedAgent xmlns="demo.org.uk/demo/AgentInventoryIdentifier">DRB</nominatedAgent>
</agentInventoryIdentity>
I need to drop the xmlns="" at the end of the opening agentInventoryIdentity element.
My code looks like this:
XmlDocument generateInventoryXml = new XmlDocument();
XmlElement root = generateInventoryXml.CreateElement ("agentInventoryReference", "demo.org.uk/demo/AgentInventoryReference");
root.SetAttribute("xmlns", "demo.org.uk/demo/AgentInventoryReference");
root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
generateInventoryXml.AppendChild(root);
XmlElement agentInventoryIdentity = generateInventoryXml.CreateElement("agentInventoryIdentity");
XmlElement shed = generateInventoryXml.CreateElement("shed", "demo.org.uk/Sequoia/AgentInventoryIdentifier");
XmlElement arrivalport = generateInventoryXml.CreateElement("arrivalPort", "demo.org.uk/demo/AgentInventoryIdentifier");
XmlElement masterAirwayBillPrefix = generateInventoryXml.CreateElement("masterAirwayBillPrefix","demo.org.uk/demo/AgentInventoryIdentifier");
XmlElement masterAirwayBillNumber = generateInventoryXml.CreateElement("masterAirwayBillNumber", "demo.org.uk/demo/AgentInventoryIdentifier");
XmlElement nominatedAgent = generateInventoryXml.CreateElement("nominatedAgent", "demo.org.uk/demo/AgentInventoryIdentifier");
shed.InnerText = shedCode;
arrivalport.InnerText = arrivalPort;
masterAirwayBillPrefix.InnerText = awbPrefix;
masterAirwayBillNumber.InnerText = awbNumber;
nominatedAgent.InnerText = agent;
agentInventoryIdentity.AppendChild(shed);
agentInventoryIdentity.AppendChild(arrivalport);
agentInventoryIdentity.AppendChild(masterAirwayBillPrefix);
agentInventoryIdentity.AppendChild(masterAirwayBillNumber);
agentInventoryIdentity.AppendChild(nominatedAgent);
generateInventoryXml.DocumentElement.AppendChild(agentInventoryIdentity);
GenerateInventoryXml = generateInventoryXml.InnerXml;
Try this:
XmlElement agentInventoryIdentity = generateInventoryXml.CreateElement("agentInventoryIdentity",generateInventoryXml.DocumentElement.NamespaceURI);`
Or
XmlElement agentInventoryIdentity = generateInventoryXml.CreateElement("agentInventoryIdentity","demo.org.uk/demo/AgentInventoryReference");
So I have this XML:
<?xml version="1.0" encoding="utf-8"?>
<M_ChucVu>
<ChucVu>
<MaChucVu>1
</MaChucVu>
<TenChucVu>
</TenChucVu>
</ChucVu>
<ChucVu>
<MaChucVu>2
</MaChucVu>
<TenChucVu>
</TenChucVu>
</ChucVu>
<ChucVu>
<MaChucVu>23</MaChucVu>
<TenChucVu>12</TenChucVu>
</ChucVu>
<ChucVu>
<MaChucVu>44</MaChucVu>
<TenChucVu>44</TenChucVu>
</ChucVu>
</M_ChucVu>
and I want to retrieve the ChucVu tags that contain an empty TenChucVu tag so the result is this:
<ChucVu>
<MaChucVu>1
</MaChucVu>
<TenChucVu>
</TenChucVu>
</ChucVu>
<ChucVu>
<MaChucVu>2
</MaChucVu>
<TenChucVu>
</TenChucVu>
</ChucVu>
XDocument doc = ...;
var query = doc.XPathSelectElements("//ChucVu[TenChucVu='']");
Another XPath that should work
/M_ChucVu[./ChucVu/TenChucVu='']
for example
var doc = new XmlDocument();
doc.LoadXml(yourXmlString);
var elem = doc.DocumentElement;
var sel = elem.SelectNodes("/M_ChucVu[./ChucVu/TenChucVu!='']");
// print or use sel.InnerXml
The XPath you need is:
/*/ChucVu[not(string(TenChucVu))]
or
/*/ChucVu[string-length(TenChucVu)=0]
You can as:
string xparth = String.Format("//ChucVu[TenChucVu='{0}']", '');
XmlDocument doc = new XmlDocument();
doc.Load("Xml");
XmlElement root = doc.DocumentElement;
XmlNode node = root.SelectSingleNode(xparth);
XmlNodeList list = root.SelectNodes(xparth);
If I want to add, update or delete node in the xml using c#, how can it be done? My xml is shown below. I dont want transactionID node. I want to add a node called <Transformation>XML</Transformation> after corelationID node.
<?xml version="1.0" ?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>HMRC-VAT-DEC</Class>
<Qualifier>poll</Qualifier>
<Function>submit</Function>
<TransactionID />
<CorrelationID>1B93D48C02D740C6B79DE68A27F3ADE5</CorrelationID>
<ResponseEndPoint PollInterval="10">https://secure.dev.gateway.gov.uk/poll</ResponseEndPoint>
<GatewayTimestamp>2011-04-05T07:41:43.018</GatewayTimestamp>
</MessageDetails>
<SenderDetails />
</Header>
<GovTalkDetails>
<Keys />
</GovTalkDetails>
<Body />
</GovTalkMessage>
The easiest thing to use would be LINQ to XML. For example:
XDocument doc = XDocument.Load("file.xml");
XNamespace ns = "http://www.govtalk.gov.uk/CM/envelope";
// Remove TransationID
XElement transactionElement = doc.Descendants(ns + "TransactionID").Single();
transactionElement.Remove();
// Add XML:
XElement correlationElement = doc.Descendants(ns + "CorrelectionID").Single();
XElement newElement = new XElement(ns + "XML");
correlationElement.AddAfterSelf(newElement);
// Save back
doc.Save("new-file.xml");
//Load the XML
XmlDocument documentXML = new XmlDocument();
documentXML.Load(Server.MapPath("AddDeleteUpdate.xml"));
XmlNamespaceManager xmlns = new XmlNamespaceManager(documentXML.NameTable);
xmlns.AddNamespace("bk", "http://www.govtalk.gov.uk/CM/envelope");
//Identify the parent node i.e <MessageDetails>
XmlNode nodeMessage = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails", xmlns);
//Delete the node.
XmlNode nodeTransactionID = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails/bk:TransactionID", xmlns);
nodeMessage.RemoveChild(nodeTransactionID);
//Create the new XML noded to be added.
XmlNode controlAttrNode = null;
controlAttrNode = documentXML.CreateElement("Transformation");
controlAttrNode.InnerText = "XML";
controlAttrNode.Attributes.RemoveAll();
//Get the node object to where it need to be added.
XmlNode nodeCorrelation = documentXML.SelectSingleNode("//bk:GovTalkMessage/bk:Header/bk:MessageDetails/bk:CorrelationID", xmlns);
//Insert the node after.
nodeMessage.InsertAfter(controlAttrNode, nodeCorrelation);
documentXML.Save(Server.MapPath("AddDeleteUpdate.xml"));
You'll need
XMLNode.InsertAfter(newChildNode,referenceChildNode)
This should kickstart you:
http://msdn.microsoft.com/en-US/library/system.xml.xmlnode.insertafter%28v=VS.80%29.aspx