将 XML XElement 的本地名称传输到字符串
Transfer the local name of a XML XElement to string
嗨,
我是 C# 编程的新手,所以这可能是一个简单的问题。
我尝试使用 XDocument 读取 XML 文件,并将元素的本地名称写为字符串。
对于输出,我使用 SiemensNX 的列表 window,但所有其他输出 window 或 txt 文件都是合适的。
这是输入-XML:
<?xml version="1.0" encoding="utf-8"?>
<Rootlvl>
<Lvl_1>
<Lvl_2/>
</Lvl_1>
</Rootlvl>
这是 C# 代码:
using System.Xml.Linq;
using NXOpen;
namespace XmlElementName
{
class Program
{
private static ListingWindow lw = s.ListingWindow;
public static void Main()
{
string XmlFilePath = @"C:\Users\XXX\Desktop\TestXML.xml"; //XML path
string testnode = "Lvl_2"; //local name of a optional XML element
lw.Open(); //open NX listinwindow for output
//=============LoadXmlFile================
//get main Input
XDocument xml = XDocument.Load(XmlFilePath); //load XmlFile
//====================WriteOutElementName====================
XElement node;
if (testnode == null) //if no optional Element name --> take root element of XML
{
node = xml.Root;
}
else
{
// Find node to passed string "testnode" --> here "Lvl_2"
node = xml.Element(testnode);
}
lw.WriteLine("Test"); //Test if output works --> !yes it works
if(node != null)
{
string output = node.Name.LocalName;
//local name of XElement-variable "node" to string
lw.WriteLine(output); //output the local name of variable "node"
}
else
{
lw.WriteLine("Element with Name = " + testnode + "not found")
}
}
}
}
如果变量 testnode = "Lvl_2" 的输出应该是:
Test
Lvl_2
如果变量 testnode = null 的输出应该是:
Test
Rootlvl
VS 调试器告诉我
node = xml.Element(testnode); //testnode = Lvl_2
在 XML 中找不到名称为 "Lvl_2" 的元素。所以它将 "node" 设置为 "Null" 并抛出以下异常:
"System.NullReferenceException: Object reference not set to an instance of an object."
但我知道 "Lvl_2" 是 XML 的子元素。我该怎么做才能在 XML 中找到此元素 "Lvl_2"?
我应该在这一行中更改什么
node = xml.Element(testnode)
按名称查找元素?
感谢大家的帮助。
如果你放置一个不存在的测试节点 xml 然后节点为空并导致异常,请在打印前进行检查
if (node!=null){
string output = node.Name.LocalName;
//local name of XElement-variable "node" to string
lw.WriteLine(output); //output the local name of variable "node"
}
根据MSDN:
Method XDocument.Element(XName) gets the first (in document order)
child element with the specified XName.
所以,在你的情况下,
node = xml.Element(testnode)
returns null
如果 testnode = "Lvl_2"
,因为 Lvl_2 不是 xml 的子元素文档(只有根节点Rootlvl
被认为是这个上下文的子元素)。
尝试使用 Descendants 方法:
node = xml.Descendants(testnode).FirstOrDefault();
感谢@jdweng,
我进行了以下更改。现在可以了。
using system.linq;
并替换
node.xml.Element(testnode);
和
node = xml.Descendents(testnode).FirstOrDefault();
嗨, 我是 C# 编程的新手,所以这可能是一个简单的问题。 我尝试使用 XDocument 读取 XML 文件,并将元素的本地名称写为字符串。
对于输出,我使用 SiemensNX 的列表 window,但所有其他输出 window 或 txt 文件都是合适的。
这是输入-XML:
<?xml version="1.0" encoding="utf-8"?>
<Rootlvl>
<Lvl_1>
<Lvl_2/>
</Lvl_1>
</Rootlvl>
这是 C# 代码:
using System.Xml.Linq;
using NXOpen;
namespace XmlElementName
{
class Program
{
private static ListingWindow lw = s.ListingWindow;
public static void Main()
{
string XmlFilePath = @"C:\Users\XXX\Desktop\TestXML.xml"; //XML path
string testnode = "Lvl_2"; //local name of a optional XML element
lw.Open(); //open NX listinwindow for output
//=============LoadXmlFile================
//get main Input
XDocument xml = XDocument.Load(XmlFilePath); //load XmlFile
//====================WriteOutElementName====================
XElement node;
if (testnode == null) //if no optional Element name --> take root element of XML
{
node = xml.Root;
}
else
{
// Find node to passed string "testnode" --> here "Lvl_2"
node = xml.Element(testnode);
}
lw.WriteLine("Test"); //Test if output works --> !yes it works
if(node != null)
{
string output = node.Name.LocalName;
//local name of XElement-variable "node" to string
lw.WriteLine(output); //output the local name of variable "node"
}
else
{
lw.WriteLine("Element with Name = " + testnode + "not found")
}
}
}
}
如果变量 testnode = "Lvl_2" 的输出应该是:
Test
Lvl_2
如果变量 testnode = null 的输出应该是:
Test
Rootlvl
VS 调试器告诉我
node = xml.Element(testnode); //testnode = Lvl_2
在 XML 中找不到名称为 "Lvl_2" 的元素。所以它将 "node" 设置为 "Null" 并抛出以下异常:
"System.NullReferenceException: Object reference not set to an instance of an object."
但我知道 "Lvl_2" 是 XML 的子元素。我该怎么做才能在 XML 中找到此元素 "Lvl_2"?
我应该在这一行中更改什么
node = xml.Element(testnode)
按名称查找元素?
感谢大家的帮助。
如果你放置一个不存在的测试节点 xml 然后节点为空并导致异常,请在打印前进行检查
if (node!=null){
string output = node.Name.LocalName;
//local name of XElement-variable "node" to string
lw.WriteLine(output); //output the local name of variable "node"
}
根据MSDN:
Method XDocument.Element(XName) gets the first (in document order) child element with the specified XName.
所以,在你的情况下,
node = xml.Element(testnode)
returns null
如果 testnode = "Lvl_2"
,因为 Lvl_2 不是 xml 的子元素文档(只有根节点Rootlvl
被认为是这个上下文的子元素)。
尝试使用 Descendants 方法:
node = xml.Descendants(testnode).FirstOrDefault();
感谢@jdweng,
我进行了以下更改。现在可以了。
using system.linq;
并替换
node.xml.Element(testnode);
和
node = xml.Descendents(testnode).FirstOrDefault();