简体   繁体   中英

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?

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.

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" ...

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM