You must Sign In to post a response.
  • Category: XML

    C# How read xml, XML Syntax when Using Colon (:), in Tags

    c# How read xml, XML Syntax when Using Colon (:) in Tags

    i want to read this
    example:

    <?xml version="1.0" encoding="utf-8"?>
    <feed xml:base="https://online.marorka.com/ODataService.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <id>https://online.marorka.com/Odataservice.svc/Ships</id>
    <title type="text">Ships</title>
    <updated>2014-12-15T06:27:45Z</updated>
    <link rel="self" title="Ships" href="Ships" />
    <entry>
    <id>https://online.marorka.com/ODataService.svc/Ships(599)</id>
    <category term="Portal_Dev_TestDbModel.Ship" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" title="Ship" href="Ships(599)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Company" type="application/atom+xml;type=entry" title="Company" href="Ships(599)/Company" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ShipPivots" type="application/atom+xml;type=feed" title="ShipPivots" href="Ships(599)/ShipPivots" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Voyages" type="application/atom+xml;type=feed" title="Voyages" href="Ships(599)/Voyages" />
    <title />
    <updated>2014-12-15T06:27:45Z</updated>
    <author>
    <name />
    </author>
    <content type="application/xml">
    <m:properties>
    <d:Id m:type="Edm.Int32">599</d:Id>
    <d:Name>Jag Lok</d:Name>
    <d:CompanyId m:type="Edm.Int32">80</d:CompanyId>
    <d:SerieId m:type="Edm.Int32" m:null="true" />
    <d:CargoId m:type="Edm.Int32">2</d:CargoId>
    <d:OnboardDatabaseVersion>3.5.1</d:OnboardDatabaseVersion>
    <d:OnboardBinaryVersion>3.5.1.11954</d:OnboardBinaryVersion>
    </m:properties>
    </content>
    </entry>

    </feed>
  • #754816
    Hi,

    can u try in this way, i thin its helpfor uu.
    xml are like:

    <rss xmlns:g="http://base.google.com/ns/1.0" xmlns:receipt="urn:receipt">
    <channel>
    <item>
    <g:id></g:id>
    <g:google_product_category />
    <g:image_link>
    </g:image_link>
    </item></channel></rss>

    2: In C#:

    XNamespace g = "http://base.google.com/ns/1.0";
    XElement rss = XElement.Parse(@"<rss xmlns:g=""http://base.google.com/ns/1.0"" xmlns:receipt=""urn:receipt""> </rss>");
    rss.Add(new XElement("channel",
    new XElement("item",
    new XElement(g + "id"),
    new XElement(g + "google_product_category"),
    new XElement(g + "image_link"),
    new XElement(g + "image_link")
    )));


    Thanks,
    chitarnjan

  • #754858

    You can read using XML reader tag by tag.

    if you want to read using XPATH that is also possible.



    sing System;
    using System.Xml;

    namespace ReadXML
    {
    class XMLRead
    {
    static void Main(string[] args)
    {
    XmlTextReader reader = new XmlTextReader ("Anil.xml");
    while (reader.Read())
    {
    switch (reader.NodeType)
    {
    case XmlNodeType.Element:
    Console.Write("Element: " + reader.Name);
    Console.WriteLine(">");
    break;
    case XmlNodeType.Text:
    Console.WriteLine ("Value:" + reader.Value);
    break;
    case XmlNodeType.EndElement:
    Console.Write("Last Element" + reader.Name);
    break;
    }
    }
    Console.ReadLine();
    }
    }
    }


    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #755775
    @umesh
    The easiest and fastest way to get <m:properties> node and its subnodes is to use System.Xml.XPath:

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(yourFileName); // or: xmlDocument.LoadXml(yourXmlString);

    // this expression below gets all "properties" nodes which parent "content" has an attribute "type" with a value = "application/xml":
    string xpathExpression = "/feed/entry/content[@type='application/xml']/m:properties";

    // to recognise the xml namespaces
    XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
    namespaceManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

    // get the node
    XmlNodeList propertiesNodes = xmlDocument.SelectNodes(xpathExpression, namespaceManager);

    // now get the subnodes...


  • Sign In to post your comments