I am working on the XML files in C#.
I want to extact the name space and do some maniplations.
say my xml file looks like this.
<Content xmlns="http://ABCD.com/sdltridion/schemas/XXXXX">
<first>ABCD</first>
<second>DCEF</second>
</Content>
I want to extract Xml namespace from the root tag, ang get the value of XXXXX.
Output needed: XXXXX
Can any one help regarding this.
Thank you.
Try this:
var xdoc = XDocument.Parse(xml);
var ns = xdoc.Root.Name.Namespace.NamespaceName;
var value = new Uri(ns).Segments.LastOrDefault();
You can try XNamespace class
XNamespace ns = XNamespace.Get("http://ABCD.com/sdltridion/schemas/XXXXX");
var result = XElement.Load("URL").Descendants(ns + "NODENAME");
Thanks
Deepu
Related
I have a template XML that looks something similar to this
<a:Demographics>
<b:Id>
<c:IdValue></c:IdValue>
<c:IdScheme></c:IdScheme>
<c:IdType></c:IdType>
</b:Id>
</a:Demographics>
Using Visual Studio and c#, How can I parse the xml, then based on my current record, add my own data into c:IdValue, c:IdScheme and c:IdType as these are required xml fields. They have been left blank in the template so that my the program will fill them I then need to save that as a new xml file but that should be straightforward once the values have been added.
I am open to using any library that can get the job done!
A valid XML must have namespace prefix declaration somewhere, I assume your XML has it. Then you can do something like this using XDocument :
XDocument doc = XDocument.Load("path_to_xml_file.xml");
XNamespace a = "http://uri.for.a.prefix.com";
XNamespace b = "http://uri.for.b.prefix.com";
XNamespace c = "http://uri.for.c.prefix.com";
XElement id = doc.Descendants(a+"Demographics")
.First()
.Element(b+"Id");
id.Element(c+"IdValue").Value = "new value here";
id.Element(c+"IdScheme").Value = "new scheme here";
id.Element(c+"IdType").Value = "new type here";
doc.Save("path_to_new_xml_file.xml");
you can use System.Xml.Linq
Load the Template
XDocument Template = XDocument.Load("Template.xml");
Modify it
void AddData(string elemName, string value)
{
XElement element = Template.Root.Descendants("elemName").First();
element.Value = value;
}
and Save it
Template.Save("newXml.xml");
I am trying to figure out how to use Linq to XML to read an XML file into my C# program. Here is the example for my question:
<node name="services" class="tridium.containers.ServiceContainer" module="coreRuntime" release="2.301.532.v1">
How do I access the name, class, module, and release information in this line? I tried .element("node").Name for the name field, but that just returns "node". All of the tutorials I can find are either too simplistic to deal with this, or deal with writing an XML file. Please help.
You can use this :
XElement rootelement = XElement.Load(#"path_to_your_file") ;
var name = rootElement.Attribute("name").Value ;
var classname = rootElement.Attribute("class").Value ;
var module = rootElement.Attribute("module").Value ;
If it is at the root, then
XDocument xdoc = XDocument.Load("data.xml");
var name= xdoc.Root.Attribute("name").Value;
var class= xdoc.Root.Attribute("class").Value;
var module= xdoc.Root.Attribute("module").Value;
I am doing something wrong can't grab shipmentreceiptlineitem to add to first document, do I need to add a namespace?
XDocument xdoc = XDocument.Load("FirstPart.xml");
xdoc.Root.Add(XDocument.Load("RepeatingPart.xml").Element("ShipmentReceiptLineItem").Elements());
xml to grab from:
<tns:ShipmentReceiptNotification xmlns:dl="urn:rosettanet:specification:domain:Logistics:xsd:schema:02.18"
xmlns:tns="urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.01">
<tns:ShipmentReceiptLineItem>
</tns:ShipmentReceiptLineItem>
</tns:ShipmentReceiptNotification>
Yes, you need to use the namespace when you try to find the ShipmentReceiptLineItem element. You also need to go from the root element, otherwise your check for Element(...) would only be able to find the root element:
XDocument xdoc = XDocument.Load("FirstPart.xml");
XNamespace tns = "urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.01";
xdoc.Root.Add(XDocument.Load("RepeatingPart.xml")
.Root
.Element(tns + "ShipmentReceiptLineItem")
.Elements());
Or splitting it up further:
XDocument repeatingDoc = XDocument.Load("RepeatingPart.xml");
XNamespace tns = "urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.01";
var elementsToAdd = repeatingDoc.Root
.Element(tns + "ShipmentReceiptLineItem")
.Elements());
var mainDoc = XDocument.Load("FirstPart.xml");
mainDoc.Root.Add(elementsToAdd);
I find this a lot simpler to read than doing everything in one go. You could potentially get rid of the repeatingDoc variable and do that bit inline, but I definitely wouldn't do the whole thing inline.
I am having trouble returning data from a Linq to XML query.
I have the following XML
<sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
<publshInformation>
<Publish_Date>03/14/2013</Publish_Date>
<Record_Count>5440</Record_Count>
</publshInformation>
</sdnList>
I am trying to get the value of Publish Date using the following
XDocument xDoc = XDocument.Load(fileName);
XNamespace xNS = "http://tempuri.org/sdnList.xsd";
XNamespace xNS1 = "http://www.w3.org/2001/XMLSchema-instance";
string strCurrentDate = xDoc.Element(xNS1 + sdnList").Element("publshInformation").Element("Publish_Date").Value;
This just returns an object expected error.
I know my problem is around the namespaces (and most likely is will be a simple solutions)
Thanks
All the elements in that document are in the http://tempuri.org/sdnList.xsd namespace, so you need something like
xDoc.Element(xNS + "sdnList")
.Element(xNS + "publshInformation")
.Element(xNS + "Publish_Date").Value;
This will work:
XNamespace xNS = "http://tempuri.org/sdnList.xsd";
string strCurrentDate = xDoc.Element(xNS + "publshInformation").Element(xNS + "Publish_Date").Value;
I am having a problem parsing xml that I receive from Web Service.
The xml looks very simple:
<Result xsi:schemaLocation="urn:yahoo:developer http://developer.yahooapis.com/TimeService/V1/GetTimeResponse.xsd" type="web"><Timestamp>1320677359</Timestamp></Result>
But when I try to parse it with following code I am getting no return results.
XDocument doc = XDocument.Load("http://developer.yahooapis.com/TimeService/V1/getTime?appid=StackSolution");
var datestamp = from ds in doc.Descendants("Result")
select new { currentstamp = ds.Element("Timestamp").Value };
Is there a solution or way to parse it?
Thanks you in advance
You have a couple issues: First, the Result node isn't a descendant. It's the root. Second, you ran into the most common issue when using LINQ to XML - you forgot the namespace. The following should give you what you need:
XElement doc = XElement.Load("http://developer.yahooapis.com/TimeService/V1/getTime?appid=StackSolution");
XNamespace ns = "urn:yahoo:developer";
var datestamp = from ds in doc.DescendantsAndSelf(ns + "Result")
select new { currentstamp = ds.Element(ns + "Timestamp").Value };
Note, this produces an IEnumerable. If you only want the datestamp, consider using FirstOrDefault instead. You may be able to make this simpler by just doing the following:
XElement doc = XElement.Load("http://developer.yahooapis.com/TimeService/V1/getTime?appid=StackSolution");
XNamespace ns = "urn:yahoo:developer";
var datestamp = doc.Element(ns + "Timestamp").Value;
This method avoids the namespace issue using LocalName (unqualified identifier).
var datestamp = doc.Root.Descendants().Where(c => c.Name.LocalName.Equals("Timestamp")).FirstOrDefault().FirstNode.ToString()