Vinoth
Vinoth

Reputation: 83

How to get the xml node value into a string

How to get the xml node value in a string.

i am getting This error

Data at the root level is invalid. Line 1, position 1.

error shown in this line

xmldoc.LoadXml(xmlFile);

my xml

<?xml version="1.0" encoding="utf-8" ?>

<UOM>

  <!-- The selected currency used will be stored here for Code reference" -->
  <ActiveCurrencyType>
    <ActiveCurrency>U.S.Dollar</ActiveCurrency>
    <ActiveCode>USD</ActiveCode>
    <ActiveSymbol>$</ActiveSymbol>
  </ActiveCurrencyType>

  <!-- The selected Dimension used will be stored here for Code reference -->
  <ActiveDimension>
    <ActiveDimensionUOM>Inches</ActiveDimensionUOM>
    <ActiveDimensionSymbol>.in</ActiveDimensionSymbol>
  </ActiveDimension>

  <!-- The selected weight used will be stored here for Code reference -->
  <ActiveWeight>
    <ActiveWeightUOM>Pounds</ActiveWeightUOM>
    <ActiveWeightSymbol>lb</ActiveWeightSymbol>
  </ActiveWeight>

</UOM>

C# code

    string xmlFile = Server.MapPath("~/HCConfig/HCUOM.xml");
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.LoadXml(xmlFile);
    XmlNodeList nodeList = xmldoc.GetElementsByTagName("ActiveDimensionSymbol");
    string ActiveDimensionSymbol = string.Empty;
    foreach (XmlNode node in nodeList)
    {
        ActiveDimensionSymbol = node.InnerText;
    }

How can I achieve this?

Upvotes: 0

Views: 221

Answers (4)

rene
rene

Reputation: 42494

You're using the wrong overload, LoadXml doesn't do what you think it does.

Use xmldoc.Load(xmFile); because that method takes an file path as input. LoadXml expects an string with xml in it.

The exception is an indicator of that mistake. What is processed is not XML, and a filepath isn't that.

After this changes the string ActiveDimensionSymbol contains .in if I run this locally.

If you want to use LoadXml you should first read your whole file in a string, for example like so:

 xmldoc.LoadXml(File.ReadAllText(xmlFile));

but is really only overhead to call File.ReadAllText if there is an method that accepts a file.

Upvotes: 2

Mostafiz
Mostafiz

Reputation: 7352

Try this code its works just fine with this xml

string xmlFile = Server.MapPath("~/HCConfig/HCUOM.xml");
XDocument doc = XDocument.Load(xmlFile );

var nodeList = doc.Descendants("ActiveDimensionSymbol");
string ActiveDimensionSymbol = string.Empty;
foreach (var node in nodeList)
{
   ActiveDimensionSymbol = node.Value;
}

Upvotes: 0

gmetax
gmetax

Reputation: 3973

As can seen here link the method that you are using to load the XML excepts xml by string not xml file. You can use XmlDocument.Load instead of XmlDocument.LoadXml

Upvotes: 0

Fredrik
Fredrik

Reputation: 2317

You can use the Descendants() method to get all XElements by certain name, found in the System.Xml.Linq namespace.

XDocument doc = XDocument.Load("XMLFile1.xml");
string[] allActiveWeightUOMs = doc.Descendants("ActiveWeightUOM").Select(o => o.Value).ToArray();

// allActiveWeightUOMs : "Pounds" ...

Upvotes: 0

Related Questions