我想從 xml 字串中洗掉 xmlns 部分并將其轉換為 json。
string test = "<Behavior xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.adlibsoftware.com\">\r\n <JobFolders>\r\n <Error>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Error>\r\n <Work>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Work>\r\n <Input>\r\n <DeleteEmptySubfolders>true</DeleteEmptySubfolders>\r\n </Input>\r\n </JobFolders>\r\n <JobFiles>\r\n <ProcessingLocation>\r\n <Server>\r\n <TransferSegmentSize unit=\"Kilobytes\">4096</TransferSegmentSize>\r\n </Server>\r\n </ProcessingLocation>\r\n <Input>\r\n <Naming>Resh</Naming>\r\n </Input>\r\n </JobFiles>\r\n</Behavior>";
我嘗試使用下面的代碼,但仍然無法洗掉它。任何幫助都會很棒!
XmlDocument doc = new XmlDocument();
doc.LoadXml(test);
foreach (var node in doc)
{
var el = node as XmlElement;
if (el != null)
{
if (el.HasAttribute("xmlns"))
{
var ns = el.GetAttribute("xmlns");
if (ns != null && ns == el.NamespaceURI)
{
el.RemoveAttribute("xmlns");
}
}
}
}
string jsonText = JsonConvert.SerializeXmlNode(doc);
我期望的輸出是:
{"Behavior":"JobFolders":{"Error":"${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}","Work":"${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}","Input":{"DeleteEmptySubfolders":"true"}},"JobFiles":{"ProcessingLocation":{"Server":{"TransferSegmentSize":{"@unit":"Kilobytes","#text":"4096"}}},"Input":{"Naming":"Resh"}}}}
我從上面的代碼收到的輸出:
{"Behavior":{"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","JobFolders":{"Error":"${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}","Work":"${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}","Input":{"DeleteEmptySubfolders":"true"}},"JobFiles":{"ProcessingLocation":{"Server":{"TransferSegmentSize":{"@unit":"Kilobytes","#text":"4096"}}},"Input":{"Naming":"Resh"}}}}
uj5u.com熱心網友回復:
問題解決了。我在這里添加了編輯過的解決方案。謝謝你的時間!
string test = "<Behavior xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.adlibsoftware.com\">\r\n <JobFolders>\r\n <Error>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Error>\r\n <Work>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Work>\r\n <Input>\r\n <DeleteEmptySubfolders>true</DeleteEmptySubfolders>\r\n </Input>\r\n </JobFolders>\r\n <JobFiles>\r\n <ProcessingLocation>\r\n <Server>\r\n <TransferSegmentSize unit=\"Kilobytes\">4096</TransferSegmentSize>\r\n </Server>\r\n </ProcessingLocation>\r\n <Input>\r\n <Naming>Resh</Naming>\r\n </Input>\r\n </JobFiles>\r\n</Behavior>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(test);
foreach (var node in doc)
{
var el = node as XmlElement;
if (el != null)
{
if (el.HasAttribute("xmlns") || el.HasAttribute("xmlns:xsi"))
{
var ns = el.GetAttribute("xmlns");
var ns1 = el.GetAttribute("xmlns:xsi");
if (ns != null && ns == el.NamespaceURI && ns1 != null && ns1!= el.NamespaceURI)
{
el.RemoveAttribute("xmlns");
el.RemoveAttribute("xmlns:xsi");
}
}
}
}
string jsonText = JsonConvert.SerializeXmlNode(doc);
uj5u.com熱心網友回復:
問題中的代碼僅洗掉每個節點的 1 個屬性。
這段代碼實際上是洗掉屬性:
string test = "<Behavior xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.adlibsoftware.com\">\r\n <JobFolders>\r\n <Error>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Error>\r\n <Work>${machine:description=RM1029-DEV:id=4FC4AF7C-AF61-45F9-BE86-49812C619F06}</Work>\r\n <Input>\r\n <DeleteEmptySubfolders>true</DeleteEmptySubfolders>\r\n </Input>\r\n </JobFolders>\r\n <JobFiles>\r\n <ProcessingLocation>\r\n <Server>\r\n <TransferSegmentSize unit=\"Kilobytes\">4096</TransferSegmentSize>\r\n </Server>\r\n </ProcessingLocation>\r\n <Input>\r\n <Naming>Resh</Naming>\r\n </Input>\r\n </JobFiles>\r\n</Behavior>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(test);
foreach (var node in doc)
{
var el = node as XmlElement;
if (el != null)
{
while (el.HasAttributes)
{
XmlAttribute item = el.Attributes[0];
if (el.HasAttribute(item.Name) && item.Name.Contains("xmlns"))
{
var ns = el.GetAttribute(item.Name);
if (ns != null )
{
el.RemoveAttribute(item.Name);
Console.WriteLine($"Remove attriute: {item.Name}");
}
}
}
}
}
string jsonText = JsonConvert.SerializeXmlNode(doc);
Console.WriteLine(jsonText);
Console.ReadLine();
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/379722.html
下一篇:xml按名稱讀取欄位
