How to Programatically read the Documentation section of a WSDL in C# - c#

i am using a WSDL file to create a the proxy class file, this service has a big Enumeration. the description for each enum value is in documentation section, how can i programatically read that section?

A WSDL file is always an XML file, so you can open it and read the elements data. For example, given the eBay Services WSDL file, you can query the documentation of the value COD of the enumeration BuyerPaymentMethodCodeType like this:
XmlDocument wsdlDoc = new XmlDocument();
wsdlDoc.Load(#"D:\temp\eBaySvc.wsdl");
XmlNamespaceManager nsMgr = new XmlNamespaceManager(wsdlDoc.NameTable);
nsMgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
XmlNode node = wsdlDoc.SelectSingleNode("//xs:simpleType[#name='BuyerPaymentMethodCodeType']/xs:restriction/xs:enumeration[#value='COD']/xs:annotation/xs:documentation", nsMgr);
string description = node.InnerText;

Related

How do I edit Node Values in an Xml File with C#

I am trying to change the values in a Farming simulator 22 savegame xml file from C# in visual studio. There are a lot of nodes so I have reduced them to make things easier. I want to know how to replace the value in the node using C# with out having to create and rebuild the xml file from scratch.
the path to the xml file is: (C:\Users\Name\Documents\My Games\FarmingSimulator2022\savegame1\careerSavegame.xml)
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<careerSavegame revision="2" valid="true">
<settings>
<savegameName>My game save</savegameName>
<creationDate>2022-05-03</creationDate>
<mapId>MapFR</mapId>
<mapTitle>Haut-Beyleron</mapTitle>
<saveDateFormatted>2022-08-22</saveDateFormatted>
<saveDate>2022-08-22</saveDate>
<resetVehicles>false</resetVehicles>
</careerSavegame>
You can use the System.Xml.Linq namespace to access the xml file. This will load the file in the memory.
There is one class inside it, XDocument, that represents the xml document.
String filePath = "C:\Users\Name\Documents\My Games\FarmingSimulator2022\savegame1\careerSavegame.xml"
XDocument xdoc = XDocument.Load(filePath);
var element = xdoc.Elements("MyXmlElement").Single();
element.Value = "foo";
xdoc.Save("file.xml");
You can set the element variable as per the one which is needed to be replaced.
Through some research I found the solution to editing the values within the nodes. In this example I only change the value of savegameName, but it will be the same for the rest.
//Routing the xml file
XmlDocument xmlsettings = new XmlDocument();
xmlsettings.Load(#"D:\careerSavegame.xml");
//Setting values to nodes through innertext
String FarmNameSetting = "Martek Farm";
XmlNode savegameNamenode =
xmlsettings.SelectSingleNode
("careerSavegame/settings/savegameName");
savegameNamenode.InnerText = FarmNameSetting;

unable to change the value of an XML tag

Here's the XML:
<PolicyChangeSet schemaVersion="2.1" username="" description="">
<Attachment name="" contentType="">
<Description/>
<Location></Location>
</Attachment>
</PolicyChangeSet>
I'm just trying to add the value "XXX" between the "location" tag.
I tried this but it isn't working:
string newValue = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(AppVars.pxCentralXMLPayloadFilePath);
node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachment/location");
node.InnerText = "XXX";
xmlDoc.Save(AppVars.pxCentralXMLPayloadFilePath);
What am I missing?!
doc.Save(#"XML FILE PATH"); //This will save the changes to the file.
Please use iteration through ChildNodes of XmlDocument - as described in walkthrough from MS Support site. According to documentation of SelectSingleNode() method:
The XmlNode should not be expected to be connected "live" to the XML
document. That is, changes that appear in the XML document may not
appear in the XmlNode, and vice versa.
Found out what the problem was CASE SENSITIVITY.

How to add Schema location and XSI dynamically to an XML file

Hi all i am creating an application to generate an XML file dynamically. In this i would like to add schemalocation and XSI to the XML Root how can i do this. I would like to add the following
xmlns="http://www.irs.gov/efile"
xsi:SchemaLocation="http://www.irs.goc/efile ReturnData941.xsd"
and
xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance"
This is my sample XML code that was generated dynamically
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode returnData = doc.CreateElement("ReturnData");
XmlAttribute documnetCount = doc.CreateAttribute("documentCount"); // after this i would like to add that schema
returnData.Attributes.Append(documnetCount);
So that i should get my XML as follows
<?xml version="1.0" encoding="UTF-8"?>
<ReturnData documentCount="" xsi:SchemaLocation="http://www.irs.goc/efile ReturnData941.xsd" xmlns="http://www.irs.gov/efile" xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance" />
I think you just need add a attribute like
XmlAttribute attr = doc.CreateAttribute("xsi", "schemaLocation", " ");
attr.Value = "http://www.irs.goc/efile ReturnData941.xsd";
returnData.Attributes.Append(attr);
I wonder if this is the best way of approaching things? Many validation APIs allow you to specify the schema location and instance document location independently, and that's likely to be more efficient than storing the schema location within the instance.
Generally I'm sceptical about xsi:schemaLocation anyway. If you're validating the instance, that's often because you don't trust it, and if you don't trust it, why should you trust its xsi:schemaLocation?

adding element to xml file from c#.net

how to add element to a xml file using asp.net& c#.net inweb application
Here's an example of adding a new element to the root node:
XDocument doc = XDocument.Load("test.xml");
doc.Root.Add(new XElement("someNode", "some node value"));
doc.Save("test.xml");
Load the document using XmlDocument class and then modify it as needed. Reference documentation and examples here.
You can use XmlDocument.CreateElement method to create it and then to append it

No Nodes Selected from Atom XML document using XPath?

I'm trying to parse an Atom feed programmatically. I have the atom XML downloaded as a string. I can load the XML into an XmlDocument. However, I can't traverse the document using XPath. Whenever I try, I get null.
I've been using this Atom feed as a test: http://steve-yegge.blogspot.com/feeds/posts/default
Calling SelectSingleNode() always returns null, except for when I use "/". Here is what I'm trying right now:
using (WebClient wc = new WebClient())
{
string xml = wc.DownloadString("http://steve-yegge.blogspot.com/feeds/posts/default");
XmlNamespaceManager nsMngr = new XmlNamespaceManager(new NameTable());
nsMngr.AddNamespace(string.Empty, "http://www.w3.org/2005/Atom");
nsMngr.AddNamespace("app", "http://purl.org/atom/app#");
XmlDocument atom = new XmlDocument();
atom.LoadXml(xml);
XmlNode node = atom.SelectSingleNode("//entry/link/app:edited", nsMngr);
}
I thought it might have been because of my XPath, so I've also tried a simple query of the root node since I knew the root should work:
// I've tried both with & without the nsMngr declared above
XmlNode node = atom.SelectSingleNode("/feed");
No matter what I do, it seems like it can't select anything. Obviously I'm missing something, I just can't figure out what. What is it that I need to do in order to make XPath work on this Atom feed?
EDIT
Although this question has an answer, I found out this question has an almost exact duplicate: SelectNodes not working on stackoverflow feed
While the C# implementation may allow default namespaces (I don't know), the XPath 1.0 spec doesn't. So, give "Atom" its own prefix:
nsMngr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
And change your XPath appropriately:
XmlNode node = atom.SelectSingleNode("//atom:entry/atom:link/app:edited", nsMngr);
Load XML from a string and lookup for any 'Errors/Error' nodes.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlResult);
XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);
nm.AddNamespace("ns", "http://somedomain.com/namespace1/2"); //ns - any name, make sure it is same in the below line
XmlNodeList errors = xmlDoc.SelectNodes("/ns:*//ns:Errors/ns:Error", nm);
-Mathulan

Categories

Resources