I want the output to be like this:
<Order xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:gml="example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="example.com http://schemas.xyz.net/gml/2.1.2/feature.xsd"
xsi:noNamespaceSchemaLocation="http://www.xyz.co.uk/xmlorders3/lig_xml_orders.xsd">
The code I am using to get this is :
xmlDocument.DocumentElement.SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
xmlDocument.DocumentElement.SetAttribute("xmlns:gml", "example.com");
xmlDocument.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlDocument.DocumentElement.SetAttribute("xsi:schemaLocation", "example.com http://schemas.opengis.com/gml/2.1.2/feature.xsd");
xmlDocument.DocumentElement.SetAttribute("xsi:noNamespaceSchemaLocation","http://www. xyz.co.uk/xmlorders3/lig_xml_orders.xsd");
But I am getting the output like this :
<Order xmlns:gml="example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="example.com http://schemas.xyz.net/gml/2.1.2/feature.xsd"
noNamespaceSchemaLocation="http://www.xyz.co.uk/xmlorders3/lig_xml_orders.xsd">
Please suggest me any other way to get the required output.
You should use another version of SetAttribute method:
xmlDocument.DocumentElement
.SetAttribute("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "example.com http://schemas.opengis.com/gml/2.1.2/feature.xsd");
i found way out. it may not be the best way. the solution is :
string xmlString = xmlDocument.InnerXml.ToString(); xmlString= xmlString.Replace("schemaLocation", "xsi:schemaLocation"); xmlString= xmlString.Replace("noNamespaceSchemaLocation", "xsi:noNamespaceSchemaLocation"); xmlDocument.LoadXml(xmlString);
// now i have the xmlDocument as i required.
Related
<?xml version="1.0" encoding="utf-8"?>
<serv:message
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:serv="http://www.webex.com/schemas/2002/06/service" xsi:schemaLocation="http://www.webex.com/schemas/2002/06/service http://www.webex.com/schemas/2002/06/service.xsd">
<header>
<securityContext>
<webExID/>
<password/>
<siteID/>
<partnerID/>
</securityContext>
</header>
<body>
<bodyContent xsi:type="java:com.webex.service.binding.training.CreateTrainingSession"
xmlns="http://www.webex.com/schemas/2002/06/service/training"
xmlns:com="http://www.webex.com/schemas/2002/06/common"
xmlns:sess="http://www.webex.com/schemas/2002/06/session"
xmlns:serv="http://www.webex.com/schemas/2002/06/service"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.webex.com/schemas/2002/06/service/training http://www.webex.com/schemas/2002/06/service/training/trainingsession.xsd">
<sess:accessControl>
<sess:sessionPassword/>
</sess:accessControl>
<sess:schedule></sess:schedule>
<metaData>
<sess:confName/>
<agenda/>
<description/>
<greeting/>
<location/>
</metaData>
<enableOptions>
<chat/>
<poll/>
<audioVideo/>
<fileShare/>
<applicationShare/>
<desktopShare/>
<annotation/>
<fullScreen/>
<voip/>
</enableOptions>
</bodyContent>
</body>
</serv:message>
Above XML is standard VILT Create Event xml and I need to populate it with proper data.
The issue is I am able to get "securityContent" element using below code and node holds total count of child elements that is 4:
var node = xmlDoc.SelectNodes("/serv:message/header/securityContext/*", GetNameSpace(xmlDoc.NameTable));
But when I try to get another node i.e. "metaData" then I get Count 0 and way to getting element is exactly same except the path to the element.
Below is sample code that I've tried but not working:
static void Main(string[] args)
{
var xmlPathh = #"C:\Users\SKMEENA\Desktop\VILT.xml";// this holds above xml
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPathh);
var node = xmlDoc.SelectNodes("/serv:message/header/securityContext/*", GetNameSpace(xmlDoc.NameTable));
var member = xmlDoc.SelectNodes("/serv:message/body/bodyContent/metaData/*", GetNameSpace(xmlDoc.NameTable));
}
static XmlNamespaceManager GetNameSpace(XmlNameTable objNameTable)
{
XmlNamespaceManager objNsManager =new XmlNamespaceManager(objNameTable);
objNsManager.AddNamespace("serv", "http://www.webex.com/schemas/2002/06/service");
objNsManager.AddNamespace("ns1", "http://www.webex.com/schemas/2002/06/service/site");
return objNsManager;
}
Anybody has any idea what is wrong with above code and how can I make it working?
The bodyContent node has a default namespace which applies to it and all its children.
You need to add it to the NamespaceManager and then use it in the XPath
var member = xmlDoc.SelectSingleNode("/serv:message/body/body:bodyContent/body:metaData", GetNameSpace(xmlDoc.NameTable));
member.OuterXml.Dump();
static XmlNamespaceManager GetNameSpace(XmlNameTable objNameTable)
{
XmlNamespaceManager objNsManager =new XmlNamespaceManager(objNameTable);
objNsManager.AddNamespace("serv", "http://www.webex.com/schemas/2002/06/service");
objNsManager.AddNamespace("ns1", "http://www.webex.com/schemas/2002/06/service/site");
objNsManager.AddNamespace("body", "http://www.webex.com/schemas/2002/06/service/training");
objNsManager.AddNamespace("sess","http://www.webex.com/schemas/2002/06/session");
return objNsManager;
}
dotnetfiddle
I am writing a method in c# that gets xml from web service with a lot of data that I do not need. I managed to select only tags that I need with this command:
var exchangeRateDataSet = xmlNew.Descendants("ExchangeRateDataSet");
Now I need to convert it to string so I can deserialize it and convert it to object of my class.
But, there is a problem. When i try to do this:
var toRead = exchangeRateDataSet.ToString();
I get this as a string, which of course isn't valid: System.Xml.Linq.XContainer+d__39
This is a piece of xml that I have:
``
<?xml version="1.0"?>
<ExchangeRateDataSet xmlns="">
<ExchangeRate xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" msdata:rowOrder="0" diffgr:id="ExchangeRate1">
<ExchangeRateListNumber>111</ExchangeRateListNumber>
<Date>14.06.2019</Date>
<CreateDate>14.06.2019</CreateDate>
<DateTo>31.12.4172</DateTo>
<ExchangeRateListTypeID>1</ExchangeRateListTypeID>
<CurrencyGroupID>2</CurrencyGroupID>
<CurrencyCode>978</CurrencyCode>
<CurrencyCodeNumChar>978</CurrencyCodeNumChar>
<CurrencyCodeAlfaChar>EUR</CurrencyCodeAlfaChar>
<CurrencyNameSerCyrl>Евро</CurrencyNameSerCyrl>
<CurrencyNameSerLat>Evro</CurrencyNameSerLat>
<CurrencyNameEng>Euro</CurrencyNameEng>
<CountryNameSerCyrl>ЕМУ</CountryNameSerCyrl>
<CountryNameSerLat>EMU</CountryNameSerLat>
<CountryNameEng>EMU</CountryNameEng>
<Unit>1</Unit>
<BuyingRate>117.6265</BuyingRate>
<MiddleRate>0.0000</MiddleRate>
<SellingRate>118.3343</SellingRate>
<FixingRate>0.000000</FixingRate>
</ExchangeRate>
<ExchangeRate xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" msdata:rowOrder="1" diffgr:id="ExchangeRate2">
<ExchangeRateListNumber>111</ExchangeRateListNumber>
<Date>14.06.2019</Date>
<CreateDate>14.06.2019</CreateDate>
<DateTo>31.12.4172</DateTo>
<ExchangeRateListTypeID>1</ExchangeRateListTypeID>
<CurrencyGroupID>3</CurrencyGroupID>
<CurrencyCode>36</CurrencyCode>
<CurrencyCodeNumChar>036</CurrencyCodeNumChar>
<CurrencyCodeAlfaChar>AUD</CurrencyCodeAlfaChar>
<CurrencyNameSerCyrl>Аустралијски долар</CurrencyNameSerCyrl>
<CurrencyNameSerLat>Australijski dolar</CurrencyNameSerLat>
<CurrencyNameEng>Australian Dollar</CurrencyNameEng>
<CountryNameSerCyrl>Аустралија</CountryNameSerCyrl>
<CountryNameSerLat>Australija</CountryNameSerLat>
<CountryNameEng>Australia</CountryNameEng>
<Unit>1</Unit>
<BuyingRate>71.9868</BuyingRate>
<MiddleRate>0.0000</MiddleRate>
<SellingRate>72.4200</SellingRate>
<FixingRate>0.000000</FixingRate>
</ExchangeRate>
``
To summarize, my question is how to convert this xml to object?
Any help would be appreciated. Thanks.
I want to get the following output:
<?xml version="1.0" encoding="Windows-1252"?>
<!DOCTYPE SUPRMRT SYSTEM "suprmrt.dtd">
I have the following code:
XmlDocument doc = new XmlDocument();
XmlDocumentType docType = doc.CreateDocumentType("SUPRMRT", "SYSTEM", "suprmrt.dtd", null);
doc.AppendChild(docType);
doc.Save(Console.out);
This produces:
<?xml version="1.0" encoding="Windows-1252"?>
<!DOCTYPE SUPRMRT PUBLIC "SYSTEM" "suprmrt.dtd">
So my question is can I get a result where PUBLIC is replaced by SYSTEM? Also, if I replace "SYSTEM" with null, I get a set of empty quotes. Can I stop that from happening?
Write it like this.
XmlDocumentType docType = doc.CreateDocumentType("SUPRMRT", null, "suprmrt.dtd", null);
Here is the MSDN Documentation
publicId
Type: System.String
The public identifier of the document type or null. You can specify a
public URI and also a system identifier to identify the location of
the external DTD subset.
I've been trying to read an xml file from an external source to get it into a dataset. I've taken the schema and compiled my class through xsd and have loaded it into the solution.
my xml:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<CAMDealerManifest AssetURL="http://www.the placewherethefilelives.com/CAMPublic/d98c8185/" DealerCode="41065" DealerGuid="bc0d0bc8-b37b-11e3-a345-ac162dbc18f8" DealerName="Ron Hibbard Toyota" ManifestId="13" ManifestURL="http://www.toyota.com/CAMPublic/d/41065-bc0d0bc8.xml" Path="41065-bc0d0bc8" ProviderId="1011" WebsiteVendorName="WorldDealer">
<CAMCampaigns count="15" dateTime="2014-09-16T17:18:40.877-07:00">
<CAMCampaign ManifestId="13" endDate="2014-12-31T11:59:59-08:00" flightDates="08/01/14 - 12/31/14" geography="National" groupName="National" id="68" name="National - Corolla Style Elevated" priority="0" required="false" startDate="2014-08-01T12:00:00-07:00">
<CAMCreative SubGroup="Corolla Style Elevated" Id="49" Name="National - Corolla Style Elevated" ImageAltText="Toyota's national banner for the stylish 2014 Corolla" ImageSearchTerms="Toyota, 2014 Corolla, Style Elevated, New Heights" Link="New Inventory" Model="Corolla" MonthYear="08-2014" Priority="3" Required="false">
<CAMCreativeSource CAMAssetCount="1" Height="409" Width="990" MediaBinAssetId="0E1C886A-C5CE-4409-AD36-7070D614A7A6" MediaBinFileName="03-14_01_2014_nat-style-elevated_990x409_0000000460_corolla_o_xta.jpg">
<CAMAsset Path="National/Corolla-Style-Elevated/National---Corolla-Style-Elevated/990/409/" Id="1273" Name="08-14_01_National-Corolla-Style-Elevated_990x409_431_Corolla_O_xta.jpg" Height="409" Width="990" assetType="JPG" Guid="b9f4d3cd-fefd-4ff3-b209-338f409e551c" Md5Hex="3e328dc32a2db9fc1f3d6a2167d3e5d2">
<CAMTracking>
<SiteCatalyst version="1.0">
<Click tmsomni.events="event28" tmsomni.products=";08-14_01_National-Corolla-Style-Elevated_990x409_431_Corolla_O_xta;;;event28=1"/>
<Impression tmsomni.events="event29" tmsomni.products=";08-14_01_National-Corolla-Style-Elevated_990x409_431_Corolla_O_xta;;event29=1;evar43=img|evar61=corolla|evar54=o|evar49={$CAM_PAGE_POSITION}"/>
</SiteCatalyst>
</CAMTracking>
</CAMAsset>
</CAMCreativeSource>
</CAMCreative>
</CAMCampaign>
</CAMCampaigns>
<CAMExpiredCampaigns count="0" dateTime="2014-09-16T17:18:40.873-07:00"/>
</CAMDealerManifest>
I can't seem to get this xml to list or put into a dataset. I'm new to deserializing xml and could use some assistance.
Simply call ReadXml with the path to the file:
var dataSet = new DataSet();
dataSet.ReadXml(string PathToFile);
Works pretty well; will even create all the relations (notice CAMDealerManifestId in the screenshot).
Your file went in with only a modification to the XML declaration -- had to change standalone value from true to yes:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
If you're reading this in as a string, you could do something like this:
var xmlString = HoweverYouGetTheString().Split(new string[] { "\r\n" }, StringSplitOptions.None);
xmlString[0] = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
using (var reader = new System.IO.StringReader(String.Join("\r\n", xmlString)))
{
dataSet.ReadXml(reader);
}
I'm stuck with some task here. I want to write to xml file.
Here is the file:
<?xml version="1.0" encoding="utf-8"?>
<lists>
<plannedList>
<Skift-a></Skift-a>
<Skift-b></Skift-b>
<Skift-c></Skift-c>
<Skift-d></Skift-d>
<Skift-e></Skift-e>
</plannedList>
<requestedList>
<Skift-a></Skift-a>
<Skift-b></Skift-b>
<Skift-c></Skift-c>
<Skift-d></Skift-d>
<Skift-e></Skift-e>
</requestedList>
</lists>
then i want to use c# and for example dataset or an other method to add a jobID under the loggedIn Skift. so if Skift-a is logged on, and press a button, i want the xml file to look like this:
<?xml version="1.0" encoding="utf-8"?>
<lists>
<plannedList>
<Skift-a></Skift-a>
<Skift-b></Skift-b>
<Skift-c></Skift-c>
<Skift-d></Skift-d>
<Skift-e></Skift-e>
</plannedList>
<requestedList>
<Skift-a></Skift-a>
<jobID>1<jobID/>
<Skift-b></Skift-b>
<Skift-c></Skift-c>
<Skift-d></Skift-d>
<Skift-e></Skift-e>
</requestedList>
</lists>
And here is what I've tried so far:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(this.Page.Server.MapPath("~/StoredData/JobsByUsers.xml"));
XmlNode Skift = xmldoc.SelectSingleNode("/requestedList/" + User.Identity.Name.ToString());
XmlNode newJobID = xmldoc.CreateNode(XmlNodeType.Element, "jobID", null);
newJobID.InnerText = topID.ToString();
Skift.AppendChild(newJobID);
xmldoc.Save(this.Page.Server.MapPath("~/StoredData/JobsByUsers.xml"));